Sorting User Defined Objects Part 2

The following two tabs change content below.
Prasad Kharkar is a java enthusiast and always keen to explore and learn java technologies. He is SCJP,OCPWCD, OCEJPAD and aspires to be java architect.

Latest posts by Prasad Kharkar (see all)

In the first part of the tutorial, we learned how we can use Comparable interface for sorting user defined objects.
But there are some points to be taken into consideration.

  • We had to implement Comparable interface by the class of which objects we needed to sort
  • In the compareTo method also, we can’t specify multiple properties by which we can sort. We could just sort Employee objects using employee ID.
  • Now what if we cannot modify the class to add compareTo method to it?
  • What if we want multiple ways by which the same object can be sorted?

Here comes the comparator interface to the rescue. We can have multiple sorting ways. For example, in our case we can sort Employee object using id, firstName and lastName also.

Let us learn how to do that with the help of the code directly.


This is a plain old java object which represents and employee. We have not implemented any interface and have not added any method to it. So how can we sort it? Let us create different classes for it.

So what we do understand from code snippets above?

  • compare()  method of Comparator interfaces takes two objects as parameters instead of  compareTo  method of comparable interface.
  • This makes possible to compare two different objects instead of calling compareTo method on some object
  • By creating different classes as we have created for sorting using id, firstName and lastName , we can keep the Employee object to be sorted unchanged. We do not need to modify the class of which objects we want to sort.


This overloaded version of Collections.sort  takes the Comparator as the second argument. Sort method calls compare method internally just like it calls compareTo method for Comparable interface.

Now run the program and you can see the following output.

Hope this helps understand how to sort custom objects in java using comparable and  comparatorinterface and the difference between them.

Share Button

Prasad Kharkar

Prasad Kharkar is a java enthusiast and always keen to explore and learn java technologies. He is SCJP,OCPWCD, OCEJPAD and aspires to be java architect.

10 thoughts on “Sorting User Defined Objects Part 2

  • November 21, 2013 at 3:29 am

    It was my interview Question but i did wrong.. there was sort employees by empName.. I missed it.

    • November 21, 2013 at 10:52 am

      If you have any questions, then feel free to ask and I will try my best to answer them.

  • November 28, 2013 at 5:22 am

    can you give example of permutation & combination .

    • November 28, 2013 at 3:38 pm

      I did not clearly understand your question.

  • January 31, 2014 at 12:26 am

    Does this example says that, first the employee is sorted by Id, then by firstname and then by lastname as the requirement , or is it just showing that all individual ways of sorting first by empid, and then showing the first name and then showing the lastname ?.

    • January 31, 2014 at 9:18 am

      Hello, Rajasekhar, Thanks for reading and commenting.
      This example is just showing all the individual ways of sorting by empid, sorting by first name and sorting by last name.

      For example, the statement Collections.sort(employees, new LastNameSorter()); instantiates an object of LastNameSorter and passes as an argument to sort method which signifies sorting should be done according to the compare() method implemented in LastNameSorter class.

      Just like LastNameSorter, other two sorters are implemented individually and called individually.
      THere is no connection between them.

      I hope this answers your question. Feel free to ask other doubts too.

  • June 5, 2014 at 6:02 pm

    Hey Prasad understood the concept about sort really well…
    But I want to know how to sort Objects in descending order with Collections.sort()?
    e.g.sorting employees in descending order of birthdate..
    And is it necessary to declare compare() method in new class always, cant we define it in original class?

    • June 6, 2014 at 12:24 am

      You can sort in reverse using the factory method described here.

      It is not necessary to define compare() method in new class but it is advisable to do so because you can create multiple sort sequences. And what if you don’t have access to java classes? In that case you cannot write compare() method in it. So you have to create a new class.

      I advise you to read the article written on sorting using comparable interface. You will know the difference

  • September 16, 2015 at 5:31 pm

    Hi!Prasad your explaination is good.but i have a question, cant we sort on integer values..because in your example you used String type..i want to sort on integer values.
    Im new to java programming.

  • November 12, 2017 at 11:53 pm

    Hi Prasad, I have a problem like i have a List<Map> now in this i have data like [{“State_ID”:”UP”,”Grs_Sale_id”:”120000.00″,”Net_Sale_id”:”130000.00″,”GRS_MRGin_ID”:”10.2″},

    here keys are some Ids like State_ID=A_123 and Grs_Sale_id= A_234 , now i want to sort this map on multiple feilds like Grs_Sale_id,Net_Sale_id and GRS_MRGin_ID like compound sorting.
    Can u please provide me the solution.


Leave a Reply

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