For determining greater, lesser values, or equality between two object variables.
Used with sorting of ArrayLists or other Collection types.
Sorting is based on comparing element values to each other by subtracting the second value from the first. This rule goes for not-number types e.g. String as well.
There can be data overflow in ordering number-type class elements. In case the first element is at the maximum of its type (e.g. Integer.MAX_VALUE) and the second element is lesser then zero (e.g. -1), subtracting the second will increase the first over its limit and the result jumps to the minimum side of the type (here: Integer.MIN_VALUE) therefore in these particular cases the order will be reversed in comparison of these two elements.
Used with a wrapper class (e.g. Integer, String) as element type.
EXAMPLE FOR SIMPLE USE OF COMPARATOR (WRAPPER ELEMENT TYPE):
In order to use Comparator in simple ways but on custom element types, the element class should know how to compare itself to other.
For this, the class must implement the Comparable interface and express the compareTo() method. There can be only one such method in a class.
When it is done, sorting as having a simple element type is possible, see previous section.
The compareTo() method reaches the fields of its own element. However, when the element class is defined to be immutable, in order to reach the fields of the same-type other element, getters must be defined within the class.
Implement this solution in case you want to introduce the sorting mechanism on element side and will not use other mechanisms in the future. Downside, this will increase the complexity of the element itself.
EXAMPLE FOR COMPARING CUSTOM ELEMENT TYPE (CUSTOM COMPARETO() METHOD):
In case there is no need for having all fields effected by comparing, use what fields you need.
In case the compareTo() method uses a wrapper field only from all the fields of the class, utilize the wrapper class comparing methods.
EXAMPLE FOR COMPARING CUSTOM ELEMENT TYPE (CUSTOM COMPARETO() METHOD ON WRAPPER FIELD):
In case the class contains a method which produces a type the compareTo() method can use, can be used in the compareTo() method as well.
In case there is priority in fields used for ordering (e.g. order by fields1, then order by field2 etc.), usage of the if statement is advised.
EXAMPLE FOR COMPARING CUSTOM ELEMENT TYPE (CUSTOM COMPARETO() METHOD, WRAPPERS AND PRIORITY):
By declaring a custom Comparator, custom and complex comparing techniques can be introduced.
A Comparator elementType must be class-type, similarly to ArrayLists, e.g. a wrapper type or a custom class-type.
A Comparator declaration must contain an overridden compare method which is responsible for the comparing itself.
The return value must be an integer.
For the return value, if it is less than zero, then they are in ascending order, if it is greater than zero, they are in descending order, zero means equality.
It is possible to declare different Comparators for any kind of ordering of a collection or more collections in the same class/application.
It is possible to create a reversed Comparator by creating a Comparator for natural order then create a reversed version from it.
Implement this solution in case you want to introduce the sorting mechanism outside the element type or want to declare more mechanisms on the same element class in the present or in the future. This also will prevent the element class growing in complexity.
EXAMPLE FOR COMPARATOR WITH ASCENDING ORDER (ANONYMOUS CLASS):
EXAMPLE FOR REVERSED COMPARATOR (ANONYMOUS CLASS):
EXAMPLE FOR COMPARATOR WITH DESCENDING ORDER (ANONYMOUS CLASS):
EXAMPLE FOR IMPLEMENTING A COMPARATOR:
EXAMPLE FOR USING COMPARATORS:
In case a class contains several fields with different types and ordering elements of this class is followed by priority (e.g. order by field1, then order by field2), Comparators can be linked in a chain.
EXAMPLE FOR CHAINED COMPARING:
In case a field type is a complex class itself, create a comparator for that field, then use that Comparator in a chain normally.