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.
- Prepare
- Java
- Data Structures
- Java Comparator
- Discussions
Java Comparator
Java Comparator
Sort by
recency
|
279 Discussions
|
Please Login in order to post a comment
This is a great exercise for learning how to implement comparators in Java! exchange play
import java.util.*;
// Player class class Player { String name; int score;
}
// Checker class that implements Comparator class Checker implements Comparator { public int compare(Player a, Player b) { // First sort by descending score if (a.score != b.score) { return b.score - a.score; // higher score comes first } // If scores are equal, sort alphabetically by name return a.name.compareTo(b.name); } }
public class Solution { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int n = scanner.nextInt();
}