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.
// Write your Checker class here
class Checker implements Comparator {
public int compare(Player a, Player b) {
if (a.score != b.score) {
return b.score - a.score;
}
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
Player[] player = new Player[n];
Checker checker = new Checker();
for(int i = 0; i < n; i++){
player[i] = new Player(scan.next(), scan.nextInt());
}
scan.close();
Arrays.sort(player, checker);
for(int i = 0; i < player.length; i++){
System.out.printf("%s %s\n", player[i].name, player[i].score);
}
}
}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Java Comparator
You are viewing a single comment's thread. Return to all comments →
import java.util.*;
// Write your Checker class here class Checker implements Comparator { public int compare(Player a, Player b) { if (a.score != b.score) { return b.score - a.score; }
} class Player{ String name; int score;
}
class Solution {
}