We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
- Sorting: Comparator
- Discussions
Sorting: Comparator
Sorting: Comparator
+ 0 comments My Swift solution:
import Foundation // Enter your code here protocol Comparator { associatedtype Item: Comparable static func compareTo(_ a: Item, _ b: Item) -> Int } final class Checker<T: Comparable>: Comparator { static func compareTo(_ a: T, _ b: T) -> Int { if a < b { return -1 } else if a > b { return 1 } else { return 0 } } } func compareTo(_ player1: (String, Int), _ player2: (String, Int)) -> Bool { let res = Checker.compareTo(player1.1, player2.1) if res == 0 { return Checker.compareTo(player1.0, player2.0) <= 0 } return res > 0 } if let numItemsStr = readLine(), let numItems = Int(numItemsStr) { var scores = [(String, Int)]() for _ in 1...numItems { if let line = readLine() { let parts = line.split(separator: " ") let name = String(parts[0]) let score = Int(parts[1]) ?? 0 scores.append((name, score)) } } scores.sort(by: { compareTo($0, $1) }) for (name, score) in scores { print("\(name) \(score)") } }
+ 0 comments Is the stub missing for Rust? I get a compilation error that there is no
main
method.
+ 0 comments Javascript - The trick is using same sort function for both sorts. Remember to console log the result --> console.log(input.join("\n"))
function processData(input){ input = input.split("\n"); input.shift(); input.sort((a,b) => { a = a.split(" "); b = b.split(" "); if(a[1] === b[1]){ return a[0] > b[0] ? 1 : -1; }else{ return b[1] - a[1]; } } );
+ 0 comments javascript
function processData(input) { //Enter your code here let arr = input.split("\n").join(" ").split(" "); let n = arr[0]; arr.shift(); let values = []; let names = []; for(let i =0; i < arr.length;i++){ if(i % 2 == 0 || i==0){ names.push(arr[i]); } if(i % 2 == 1){ values.push(parseInt(arr[i])); } } let new_arr = [] let mini_arr = [] for(let j = 0; j < values.length; j++){ mini_arr.push(names[j]); mini_arr.push(values[j]); new_arr.push(mini_arr); mini_arr = []; } values.sort(function(a,b){return a-b}) values.reverse(); new_arr.sort(); new_arr.sort(function(a, b){return b[1]-a[1]}); for(let k = 0; k < new_arr.length; k++){ console.log(new_arr[k][0] + " "+ new_arr[k][1].toString()) } }
+ 0 comments - Java *
private static List<Person> personsList = new ArrayList<>(); public static void main(String[] args) { /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */ //System.out.println("Input persons count(divided by space):"); Scanner sc = new Scanner(System.in); int personsCount = sc.nextInt(); int i = 0; while(i < personsCount) { String data = sc.nextLine(); if (!data.equals("")) { String[] splittedData = data.split(" "); String name = splittedData[0]; Integer score = Integer.parseInt(splittedData[1]); personsList.add(new Person(name, score)); i++; } } sc.close(); Collections.sort(personsList, new PersonComparator()); for (Person p : personsList) { System.out.println(p); } } } class PersonComparator implements Comparator<Person> { public int compare(Person o1, Person o2) { if ((o2.getScore() - o1.getScore()) == 0) { return o1.getName().compareTo(o2.getName()); } return o2.getScore() - o1.getScore(); } } class Person { private String name; private Integer score; Person(String name, Integer score) { this.name = name; this.score = score; } public void setName(String name) { this.name = name; } public void setScore(Integer score) { this.score = score; } public String getName() { return this.name; } public Integer getScore() { return this.score; } public String toString() { return this.name + " " + this.score; } }
Load more conversations
Sort 478 Discussions, By:
Please Login in order to post a comment