Chapter
Comparators
From ComputerScienceOne by Dr Chris Bourke
Concepts
- How does a Comparator determine the order of two elements?
"A generic sorting algorithm such as Quick Sort specifies how to sort elements, but it does not specify how those elements are ordered. Quick Sort needs to know when two elements, a and b, are in orde
- What is the difference between Comparable and Comparator in Java?
"Each of Java’s standard types implements something similar called the Comparable interface, which specifies a compareTo() method with the same basic contract. Strings use lexicographic ordering, whil
- How can anonymous classes define custom Java comparators?
"A Comparator can be created as an anonymous class, allowing a class to be defined and instantiated inline without declaring a named class or placing it in a separate .java source file. For example, a
- How can Comparator order Student objects by different fields?
"Comparators can define several orderings for Student objects based on one or more member variables. A comparator named byName orders students by last name and then first name: if the last names are e