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

How does a Comparator determine the order of two elements? | ComputerScienceOne | Bifalgorithm | Bifalgorithm