Sorting objects in Java
In java for sorting object using data members, there are two interfaces
- Comparable
- Comparator
Comparable
Comparable<T>
interface present in java.lang package
. It has only one method compareTo(). A Comparable object is used to compare itself with other homogenous objects. The class which need to be sorted itself should implement the Comparable interface and compulsorily provide the implementation for the compareTo() method.
For example, we have a Book class with the fields like name, author and publishingYear. Now, we wish to sort a List of Books by their publishing year. So, we Book class has to implement Comparator and provide the implementation for compareTo()
method.
compareTo() return values
- compareTo() returns negative value means there will be no swap
- compareTo() returns positive value means there will be a swap
- compareTo() returns 0 means there will be no swap
In the below example, we have sorted a list of Book object based on their publishing years.
Now that our Book class is sortable and we have also defined our sorting result, we need to call the sort() method on the collection of Books.
sort() method
sort() is a utility method from java.util.Collections
class. It’s an overloaded method. Different sort() methods of Collections are,sort(List<T> list, Comparator<? super T> c)
sort(List<T> list)
So, let’s sort a Collection of Book objects with sort() method,
Book [author=Paulo Coelho, name=The Alchemist, publishingYear=2019]
Book [author=Yuval Noah Harari, name=Sapians, publishingYear=2014]
Book [author=Carol Dweck, name=MindSet, publishingYear=2008]
Book [author=Yeari Mina, name=Alchemist, publishingYear=2017]
After sorting….
Book [author=Carol Dweck, name=MindSet, publishingYear=2008]
Book [author=Yuval Noah Harari, name=Sapians, publishingYear=2014]
Book [author=Yeari Mina, name=Alchemist, publishingYear=2017]
Book [author=Paulo Coelho, name=The Alchemist, publishingYear=2019]
All the wrapper classes in Java implements Comparable
Comparator
java.util.Comparator<T> is used to sort a collection of different types like List<Object>. It means Comparator is like “I can compare myself with other objects of same/different type”.
Unlike Comparable the class which needs to be sorted doesn’t need to implement Comparator. A separate class has to implement the Comparator interface and define compare(T o1, T o2)
method.
Let’s sort the collection of Book objects with Comparator.
Book [author=Paulo Coelho, name=The Alchemist, publishingYear=2019]
Book [author=Yuval Noah Harari, name=Sapians, publishingYear=2014]
Book [author=Carol Dweck, name=MindSet, publishingYear=2008]
Book [author=Yeari Mina, name=Alchemist, publishingYear=2017]
After sorting….
Book [author=Carol Dweck, name=MindSet, publishingYear=2008]
Book [author=Yuval Noah Harari, name=Sapians, publishingYear=2014]
Book [author=Yeari Mina, name=Alchemist, publishingYear=2017]
Book [author=Paulo Coelho, name=The Alchemist, publishingYear=2019]
We can also perform this sorting by lambda expression and without creating an extra class.
Comparing using two fields at the same time
class SortByNameAndYear implements Comparator<Book>{ public int compare(Book o1, Book o2) { int nameCompare = o1.getName().compareTo(o2.getName()); int yearCompare = o1.getYear() - o2.getYear(); if (nameCompare == 0) return ((yearCompare == 0)? nameCompare : yearCompare); else return nameCompare; }}