Comparable example in java

Comparable interface imposes a total ordering on the objects of each class that implements it. This ordering is referred to as the class’s natural ordering, and the class’s compareTo method is referred to as its natural comparison method.

Lists and arrays of objects that implement this interface can be sorted automatically by Collections.sort and Arrays.sort methods. Objects that implement this interface can be used as keys in a sorted map or elements in a sorted set, without the need to specify a comparator.

How to use Comparable?

There is Comparable interface in Java to support the concepts, and this interface has one method to be implemented by user, and the method is:

java.lang.Comparable: int compareTo(Object obj)

This method compares this object with obj object. Returned int value has the following meanings.

positive – this object is greater than obj

zero – this object equals to obj

negative – this object is less than obj

Example of Comparable

Here we suppose that an Object Person has to sort on basic of personId in the list. The person object should implement the Comparable interface, and Person object should override the compareTo method. the code for Person class as.

public class Person implements Comparable<person> {
    private int personId;
    private String name;
    private int age;
    public int compareTo(Person obj) {
        return this.personId - obj.personId;
    public Person(int personId, String name, int age) {
        this.personId = personId; = name;
        this.age = age;
    public int getPersonId() {
        return personId;
    public void setPersonId(int personId) {
        this.personId = personId;
    public String getName() {
        return name;
    public void setName(String name) { = name;
    public int getAge() {
        return age;
    public void setAge(int age) {
        this.age = age;

A method getPersons add the list of Person objects with randomly personId as below code.

public static List<person> getPersons() {
        List<person> list = new ArrayList<person>();
        list.add(new Person(4, "Dinesh", 26));
        list.add(new Person(5, "Alex", 28));
        list.add(new Person(7, "Murli", 40));
        list.add(new Person(1, "Shekar", 21));
        list.add(new Person(8, "Rakesh", 33));
        list.add(new Person(2, "Akshay", 20));
        list.add(new Person(3, "Manish", 22));
        list.add(new Person(6, "Ramlal", 32));
        return list;

Now you use this list to sort by Collctions.sort method, you will find that the Perdon objects are sorted based on personId. The example shown below.

public static void main(String[] args) {
        List<person> list = getPersons();
        Collections.sort(list);// Sort method
        System.out.println("PersonId \tName \tAge");
        for (Person e : list) {
            System.out.println(e.getPersonId() + " \t" + e.getName() + " \t" + e.getAge());

The output of the above code will be as:

PersonId    Name    Age
1   Shekar  21
2   Akshay  20
3   Manish  22
4   Dinesh  26
5   Alex    28
6   Ramlal  32
7   Murli   40
8   Rakesh  33