Making Java Comparators Easy : For Beginners

If you have a Collection, let’s say Array List of <Integers> and I ask you to sort it. You would say it’s simple and you would probably do something like.
Collections.sort(arraylist);//Great! You are Pretty Smart!
Now let’s say you have an Array List of <Person> (refer to the Person Class below) and this class has 3 properties, Person’s name, age and height followed by the Constructors, Getters and Setter methods.
// Array List Of Person ObjectsArrayList<Person> personArrayList = new ArrayList<>();
Now if I ask you to sort it, you would think and ask Sort the list but on what parameter? You cannot do something like :
Collections.sort(listOfPeople);Since you asked such a good question of How to sort our Collection of Person object. Now I am specify the criteria.
1. Sort the Person by their increasing order of Age.
2. If two of them have same age, then sort them by their decreasing order of height.
3. If they have same age and height, then sort them by their names alphabetically.
4. If they have same age, height and name, leave them unsorted.
So here is where the Comparators Interface comes to rescue. I am here to get you familiarize with it.
Since its an Interface, we cannot instantiate it. We have several ways to go about using it. Most simplest one of them is through Anonymous Classes.
collectionName.sort( // Implementation of Comparator Interface
// in an anonymous class
// This helps tell the sort method on how to Sort your
// custom class Collection);
Code : If you want to sort Person only on basis of their age.
For case when age is same, we need to extend our logic in a little more depth as follows.
Code : If you want to sort Person only on basis of their age, height and name as stated previously.
Lets say we inserted into person Array List as :
ArrayList<Person> personArrayList = new ArrayList<>();
personArrayList.add(new Person("Sam", 20, 180));
personArrayList.add(new Person("Mike", 27, 170));
personArrayList.add(new Person("Rob", 29, 160));
personArrayList.add(new Person("Jack", 29, 190));
personArrayList.add(new Person("Bob", 21, 170));
personArrayList.add(new Person("Bean", 21, 176));
personArrayList.add(new Person("Bob", 21, 170));
personArrayList.add(new Person("Jim", 30, 160));
personArrayList.add(new Person("Jimmy", 30, 160));And after calling the above mentioned sort method, the ArrayList gets sorted. The output you get is :
Age : Height : Name
20 : 180 : Sam
21 : 176 : Bean
21 : 170 : Bob
21 : 170 : Bob
27 : 170 : Mike
29 : 190 : Jack
29 : 160 : Rob
30 : 160 : Jim
30 : 160 : JimmyAnd that is how you sort Custom Collections using Comparator Interface.
P.S. We used custom collectionName.sort()method, which is possible with help of Comparators.
To make your own custom Collections.sort(collectionName) method, we need to learn about Comparables. In some other short tutorial.
The main advantage of Comparators is that we don’t have to change the Person Class at all. We just wrote the logic of our own sort method at the time we called the sort() method. This proves a lot helpful when the Data class is not accessible / hidden / abstracted from the developer and he has to sort it
Comparables need to be implemented in your Custom Data Class (Person Class here) that is the main difference between Comparables and Comparators. More about Comparables in my next tutorial.
Go on, play around with your own custom classes and see on how many parameters you can sort them on.
Need a fun exercise? Create a custom Laptop Class, which has 3 fields : RAM, Price, Name. Define the required constructors, getters and setters. Sort the Laptop according to your own defined logic.
Till Next Time! DeeJay out!
