Sorting User Defined Objects Part 1

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)

Hello all, in this tutorial we are going to learn sorting user defined objects in java. Well, we all have been sorting strings and numbers so far but how can I sort Employee objects? What if I need some order to be defined for employees? Java provides a way to implement this. Here we will learn about the comparable and comparator interfaces.

We can directly go with the example. Here is the Employee class.

Lets understand first what is going on in this class.

  • Notice the class declaration. It implements Comparable interface. The  <Employee>  means that this class will be able to sort Employee objects
  • Notice the  toString()  method. Well this is just to give a meaning to the object, so that when we print the object using System.out.println , we can see which object it really is.
  • Now the important part of sorting, we are overriding the  compareTo ()  method which will be responsible for sorting. Here we write the logic to sort objects.
  • we are sorting employees using the id.

Now what does the  compareTo  method actually do? According to the official java documentation, the compareTo method returns

  • a negative integer if anObject < anotherObject
  • zero if anObject equals anotherObject
  • a positive integer if anObject > anotherObject

Now that we have created Employee class and overridden the necessary methods, we will write a class to test whether we can sort or not. Here it goes.


Well, just run EmployeeSort program and you can see the output as follows.

Well.. so far so good, but using this method has some flaws..Lets go to the next part of this tutorial

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.

12 thoughts on “Sorting User Defined Objects Part 1

    • July 26, 2013 at 8:35 am

      Thank you for stopping by and reading 🙂

  • August 6, 2013 at 12:59 am

    I am now not positive the place you are getting your information, but great topic.
    I must spend some time finding out much more or figuring out more.
    Thank you for great information I was searching for this
    information for my mission.

    • August 6, 2013 at 10:28 am

      Thanks for stopping by and reading 🙂 I am glad that this article helped you.

  • October 28, 2013 at 10:46 am

    I think that is one of the such a lot vital information for me.
    And i am happy studying your article. But wanna statement on few basic issues, The website taste is
    ideal, the articles is in reality great : D. Ecellent task,

  • October 29, 2013 at 12:12 pm

    An impressive share! I’ve just forwarded this onto a friend who was doing a little homework on this.

    And he in fact bought me breakfast due to the fact
    that I discovered it for him… lol. So let me reword this….
    Thanks for the meal!! But yeah, thanks for spending the time to
    discuss this subject here on your web page.

  • October 30, 2013 at 5:51 am

    Fantastic website. Lots of helpful info here. I’m sending it to several pals ans also sharing in delicious.
    And certainly, thank you in your effort!

  • October 31, 2013 at 4:14 am

    This is really interesting, You are a very skilled blogger.
    I have joined your feed and look forward to seeking more of your
    great post. Also, I’ve shared your site in my social networks!

  • November 1, 2013 at 12:09 am

    Good post. I learn something totally new aand challenging on blogs I
    stumbleupon everyday. It will always be exsciting to read through articles from
    other authors and use something from their websites.

  • November 1, 2013 at 2:51 am

    Thanks on your marveloous posting! I really enjoyed reading it, you happen to be a great author.

    I will make sure to bookmark your blog and will come back sometime soon.
    I want to encourage you to continue your
    great job, have a nice holiday weekend!


Leave a Reply

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