Skip to content

Instantly share code, notes, and snippets.

@feliperazeek
Created October 14, 2016 22:09
Show Gist options
  • Save feliperazeek/621ee35f134fa87d8c8bf1ad02289a8e to your computer and use it in GitHub Desktop.
Save feliperazeek/621ee35f134fa87d8c8bf1ad02289a8e to your computer and use it in GitHub Desktop.
HackerRank - Cracking the Code Interview - Sorting: Comparator (https://www.hackerrank.com/challenges/ctci-comparator-sorting)
public class Checker implements Comparator<Player> {
public int compare(Player p1, Player p2) {
int score = Integer.compare(p2.score, p1.score);
if (score != 0) return score;
else return p1.name.compareTo(p2.name);
}
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);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment