How to sort objects using compareTo method and Comparable interface in Java

Hello guys,

welcome to another post. In this post we are going to see how to sort our custom objects by using Comparable interface in Java. We will create a Person class and sort

its objects on the basis of age of Person we create.Code explained below

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;  

 public class Person implements Comparable {
   //defining member variables
   private int age;
   private String name;
   private String gender;  

   Person(String name, String gender, int age) {
     this.name = name;
     this.gender = gender;
     this.age = age;
   }  

   public static void main(String[] args) {
     //defining objects
     Person john = new Person("John", "Male", 20);
     Person harry = new Person("Harry", "Male", 25);
     Person stacy = new Person("Stacy", "Male", 21);  

     //adding to list
     List personList = new ArrayList();
     personList.add(john);
     personList.add(stacy);
     personList.add(harry);  

     //sorting list on the basis of age which we have defined in compareTo method
     Collections.sort(personList);  

     System.out.println();
     System.out.println(personList);
   }  

   @Override
   public int compareTo(Person o) {
     if (o.age < this.age) return 1;
     else if (o.age == this.age) return 0;
     else return -1;
   }  

   /*
   good practice and increases output readability
    */
   @Override
   public String toString() {
     return
         "{name: " + this.name +
             ", gender: " + this.gender +
             ", age: " + this.age +
             "}";
   }
 }

Here is the code explanation:

  • First create Person class with name, age and gender attributes.
  • Then implement Comparable interface with Person as parameter so that we can use Person to compare in compareTo method.
  • We override compareTo method and compare age of the Person passed as parameter and current Person object. On that basis Person objects are sorted.
  • The sorting process is performed as:

At first another Person object is passed as parameter to compareTo method. We have compared age of person to sort in ascending order. It we reverse the compare signs, the output will be in descending order. less than 0, 0 and greater than 0 values should be returned by compareTo method which indicates less than, equal and greater than respectively. We can simply write return this.age – o.age;Β to compare on age basis. While Collections.sort(personList) gets executed, the Person object on the list will be sorted which can be checked by printing the value of personList.

  • If we run this program, the output will be:

[{name: John, gender: Male, age: 20}, {name: Stacy, gender: Male, age: 21}, {name: Harry, gender: Male, age: 25}]

Please leave comments for any suggestions, questions or random stuff. See you in next one.

Cheers πŸ™‚

Advertisements

2 thoughts on “How to sort objects using compareTo method and Comparable interface in Java

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s