The process of placing a list of items in a specified manner is known as Sorting. Some common criteria for sorting are alphabetical and numerical. It can be done in an ascending manner or in descending order. We all know that Collections.sort has the ability in sorting an arrayList for example, alphabetically if they are Strings.
But the question arises what happens when there are more complex data objects containing two or more variables including String?
How would one sort a list of objects that have more than one sortable element?
Let’s take an example of a simple object which is the car. The above image represents the way a car is defined which includes its model, year, and color.
A simple framework is used that would include multiple sort options which are provided to the sorter for sorting the overall list.
However, from the above example, it can be inferred that the solution is accurately expandable. In case, the car is supposed to receive a new field, for example, the current owner, the requirement for adding the field would require tracking the sorter class file leading to the implementation of the new sort option class. Finally, recompilation of the application for redistribution will take place making it exhaustible.
Thus, for sorting complex objects the “JDK8” has completely transformed the way in comparing objects and sorting them in java. The new features in Java 8 including lambda expression and method reference have made it easier for implementing the “Comparator” and “Comparable” interfaces. It acts as a powerful method for day-to-day programming.
Comparable Interface for sorting complicated objects
Java “Comparable Interface” is helpful for ordering the objects of the user-defined class. It is found in java.lang package and includes only one method identified as “compareTo(Object)”. Thus, “Comparable Interface” offers a single sorting sequence which implies that we can sort the elements based on a single data member.
This method is used for comparing the current object with the stated object. For integer, it returns positive if the current object is “greater than” the identified object else returns a negative integer. It also returns zero if the current object is equal to the stated object. The image below sorts the list elements based on the runs. If the runs are greater than Cricketer runs it will return a positive integer else a negative one.
Disadvantages of Comparable Interface
Although “Comparable Interface” is a powerful method, however, it has some disadvantages which include:
- At any point in time sorting can be done based on only one parameter.
- Source class access should be given to outsiders.
Comparator Interface for sorting complex objects
Java “Comparator Interface” is useful for sorting an array or list of objects which are based on “custom sort order”. It imposes a “total ordering” on objects that might not have a fruitful natural ordering.
For example, for a List of cricketer objects, sorting the highest cricket scorer is based on the number of runs made by each cricketer. In such conditions, we use “Comparator Interface”.
Circumstances of using “Comparator interface”
l Sorting the array but not in “natural order”.
l Sorting an array or list of objects which does not involve modification of source code for implementing a “Comparable interface”.
l Utilising “Group by sort” on an array or list of objects based on multiple fields.
Difference between comparable and comparator
|It expects the body to compareTo methods.
A compareTo method accepts a single parameter.
It is present in java.lang package
Using comparable interface sorting can happen based on a single parameter.
Incomparable interface first object is referred to by this keyword.
The Source class will be modified.
Gets invoked automatically when Collections. sort(list) method is invoked.
|It expects the body for the comparison method.
A comparison method accepts two parameters.
It is present in java.util package.
Here sorting can be performed based on multiple parameters.
In comparator first object is not referred by this keyword.
Source class will never be modified.
Gets invoked automatically when Collections.sort(list,
comparator) the method is invoked.
Advantages of Comparator Interface
- Utilizing a comparator we can sort objects based on more than one field of a class.
- For comparator, a class doesn’t need to implement an interface for sorting on a collection of objects.