Concept
How does a Comparator determine the order of two elements?
ComputerScienceOne / Comparators
"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 order, out of order, or equivalent so it can decide which partition each element belongs in. The elements could be numbers, strings, or user-defined objects. In Java, a Comparator object determines the proper order by implementing the method public int compare(T a, T b). A Comparator<T> is parameterized to operate on any type T, and the method takes two instances whose type matches T. The method returns a negative value if a comes before b, zero if a and b are equal, and a positive value if a comes after b. There is no guarantee about the magnitude of the returned value; it does not necessarily return -1 or +1."
Related Ideas
- What is a comparator function and how does it determine element order?ComputerScienceOne · Comparator Functions
- How can anonymous classes define custom Java comparators?ComputerScienceOne · Comparators
- How can Comparator order Student objects by different fields?ComputerScienceOne · Comparators
- How do you implement a comparator for integers in C?ComputerScienceOne · Comparator Functions
- How can comparator functions order Student structures?ComputerScienceOne · Comparator Functions
- How can a Comparator sort Student objects by GPA?ComputerScienceOne · Searching
- How should a Comparator order elements in a SortedSet?ComputerScienceOne · Handling javanull values
- How does Java sort arrays and lists with comparators?ComputerScienceOne · Sorted Collections