Sort ArrayList of Objects using Comparable and Comparator interface

In the previous post, we have seen how to sort arraylist in descending order. In that post , we have sorted out the arraylist for String and Integer values. We have used Collections.sort() method to sort the list. What if, we have Objects in the arrayList instead of String and Integer values. Can we use Collections.sort() method to sort the list. Let’s understand with the example.

Why do we need Comparable and Comparator interface?

Let’s consider that we have class called Address which has properties like StreetName, City, Country etc.

Now, Let’s create an arrayList of Address Objects. It will give a compile time error and you can execute it to see the error in console.

Output:

Exception in thread “main” java.lang.Error: Unresolved compilation problem: Bound mismatch: The generic method sort(List) of type Collections is not applicable for the arguments (ArrayList). The inferred type Address is not a valid substitute for the bounded parameter > at com.testingpool.demo2.SortArrayListEx.main(SortArrayListEx.java:15)

So we get this error that type address is not a valid substitute. That means just simply sort method does not work for arraylist of objects. This can be sorted out by using interfaces comparable and comparator.

Let’s see how to use them.

Sorting ArrayList of Object with the help of Comparable Interface:

Suppose, we need to sort the arrayList on the basis of StreetNo. For that, we will implements the comparable interface and then override the CompareTo method.

Now, We can call the same program to sort the arraylist.

Output:

[ StreetNo =33, City =New York, Country =US] [ StreetNo =86, City =Bangalore, Country =India] [ StreetNo =100, City =Pune, Country =India]

Why do we need comparator when Comparable interface is doing the required job for us?

By using Comparable, we can only sort single property and that should be of integer type. For sorting multiple properties, we need to use comparator.


 

Sorting ArrayList of Object with the help of Comparator Interface:

Let’s implements the Comparator interface and perform sorting on the basis of StreetNo and City.

Output:

Sorting based on StreetNo
[ StreetNo =33, City =New York, Country =US] [ StreetNo =86, City =Bangalore, Country =India] [ StreetNo =100, City =Pune, Country =India]
Sorting based on City
[ StreetNo =86, City =Bangalore, Country =India] [ StreetNo =33, City =New York, Country =US] [ StreetNo =100, City =Pune, Country =India]

Questions/Suggestions
Have any question or suggestion for us?Please feel free to post in Q&A Forum

 

Print Friendly, PDF & Email
Sort the ArrayList in descending order
Find SubList of ArrayList
Shekhar Sharma

Shekhar Sharma

Shekhar Sharma is founder of testingpool.com. He is an automation engineer having more than 8 years of experience who loves troubleshooting in automation and finding innovative ways to solve the problems. His other leisurely activities includes playing harmonica, paintings, sketching portrait, watching movies and travelling etc.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *