Skip to content

Java Comparator & compare

1. About Comparator Interface & compare Method

In the past example on Comparable Interface, we built Natural Sorting Order for our Product class by overriding the compareTo method. We did this sorting in the Product class itself. Then we used the Product as TreeSet Items. This means, does TreeSet support only one sorting? No, we can define custom sorting also. We can define it through a user class which signs the Comparator Interface and overrides its compare method. We can define one or many custom sorting orders using the Comparator Interface. For example, Sorting the product by its name in Ascending and descending order can be done through Java Comparator Interface. In this example, we will provide one sorting for our Product class.

2. Java Comparator Interface & Custom Sorting

The Comparator interface helps to sort Java Collection frame-work. For example, Collection.sort and Arrays.sort utility methods (static) will take the Comparator object to do sorting. Java Collection framework classes which implement SortedSet and SortedMap interfaces can make use of this Comparator for keeping the items in a sorted way. This interface has a method called compare, which one can use to write their custom sorting logic. In this example, we will use this Comparator to sort the Product object by its name. Then we will tell the TreetSet to use this Comparator class.

3. The compare Method of Java Comparator

In the last example about Comparable Interface, we learnt about the compareTo method. Now, let us look at the compare method of the Comparator Interface. Below is the signature of the compare method:

The Compare Method of Comparator
The Compare Method of Comparator

From the picture, we can conclude that the compare method takes two objects that needs to be compared. In case of TreeSet, the item e1 is fixed and item e2 is changed by java algorithm. Means, Java API function like add(E e1) will make a call to compare by pushing e2 from its existing collection items. This happens till e1’s position is fixed in the TreeSet. From the return value, the collection class like TreeSet know how to order the items. Now, have a look at the below picture:

How Comparator compare Method Works
How Comparator compare Method Works

The above picture tells how item e1 is added to the existing collection of five items. The compare logic will fit the item e1 between 9 and 6. The logic here works the same way how compareTo method works. Note how the collection item e2 is placed first in the return statement to get descending order. Our Product class already has Natural Sorting Order via the Comparable interface. Now, it is time to go on with the example.

4. Code Example – compare Usage

4.1 Sorting Product Via Java Comparator

We know our Product Class is Natural Sorting Order compatible via the Comparable Interface. Now, we will setup the custom sorting order for it. First, we need to create a class which signs Comparator Interface. Then write a compare method which takes two Product object as arguments and aids the compare action by returning an integer. Below is the code:

In the above code, we define a class ProdIdDesc which implements the Comparator interface. Note the Comparator is for the Product class and you can see that in line 4 Comparator<Product>. In the compare method body, we use the ProdId member to go on with the comparison. Note, we kept the fixed part as the 2nd operand for the – operator. These operands are swapped when we compared two Products in our past example on Comparable Interface. Recall, the Comparable is for defining the Natural Sorting Order. Now, this compare method is defining the sorting in descending order of Product Id.

4.2 TreeSet using the Java Comparator

We create our TreeSet to use the Comparator created in the previous step. In the below code, at line 1, we create an instance of the Comparator ProdIdDesc. Then we give this Comparator to the TreeSet while constructing it. Now, when we add the elements to the TreeSet, it makes use compare method of Comparator in place of compareTo of the Comparable. This will end the sorting in Descending Order of Product Id. Note, you can implement as many comparator as you want. Say, for example, we can write one more Comparator which sorts the Products by its name in Ascending order or in Descending order.

Now if we add elements and print it, the TreeSet prints the collection items in Descending order of Product Id.

5. Complete Example – Java Comparator Interface

6. Watch Comparator & compare Example as YouTube

Do you like this Example? Please comment about it for others!!

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: