You are viewing a single comment's thread. Return to all comments →
import java.util.*; class Checker implements Comparator<Player> { @Override public int compare(Player o1, Player o2) { if (o1.score != o2.score) { return o2.score - o1.score; } else { return o1.name.compareTo(o2.name); } } } class Player{ String name; int score; Player(String name, int score){ this.name = name; this.score = score; } } class Solution { 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); } } }
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 →