DEV Community

Cover image for Comparable interface in Java
KIRUBAGARAN .K
KIRUBAGARAN .K

Posted on

Comparable interface in Java

The Comparable interface in Java is used to define the natural ordering of objects of a class. It enables objects to be compared and sorted automatically without using an external Comparator.

  • It contains the compareTo() method, which compares the current object with another object.
  • It is commonly used with Collections.sort() and Arrays.sort() for sorting custom objects.

public interface Comparable<T> {
int compareTo(T o);
}

Any class that implements Comparable must provide an implementation for the compareTo(T o) method.

Why Use Comparable?

Problem Without Comparable:

Let's say we have a Student class and a list of students. If we try to sort them using Collections.sort(), Java won’t know how to compare them.

import java.util.*;

class Student {
    int id;
    String name;

    Student(int id, String name) {
        this.id = id;
        this.name = name;
    }

   public String toString() {
    return "Student[id=" + id + ", name='" + name + "]";
}
}

public class Main {
    public static void main(String[] args) {

        List<Student> container = new ArrayList<>();

        Student student1 = new Student(3, "Alice");
        Student student2 = new Student(2, "Charlie");
        Student student3 = new Student(4, "Jennie");

        container.add(student1);
        container.add(student2);
        container.add(student3);

        Collections.sort(container);

        System.out.println(container); 
    }
}

Output :
Main.java:22: error: no suitable method found for sort(List<Student>)
Collections.sort(container);    

Enter fullscreen mode Exit fullscreen mode

Implementing Comparable:

To fix this, we implement Comparable and override compareTo()

Sorting Students by ID:

import java.util.*;

class Student implements Comparable<Student> {
    int id;
    String name;

    Student(int id, String name) {
        this.id = id;
        this.name = name;
    }

    @Override
public int compareTo(Student other) {
    if (this.id > other.id) return 1;
    if (this.id < other.id) return -1;
    return 0;
}

    @Override
     public String toString() {
    return "Student[id=" + id + ", name='" + name + "]";
}
}

public class Main {
    public static void main(String[] args) {
        List<Student> container = new ArrayList<>();

      Student student1 = new Student(3, "Alice");
        Student student2 = new Student(2, "Charlie");
        Student student3 = new Student(4, "Jennie");

        container.add(student1);
        container.add(student2);
        container.add(student3);

        Collections.sort(container);

        System.out.println(container);
    }
}

Output:
[1 - Bob, 2 - Charlie, 3 - Alice]

Enter fullscreen mode Exit fullscreen mode

What is happening?

implements Comparable → tells Java this class can be compared
compareTo() → defines comparison logic

Positive → current object is greater
Negative → current object is smaller
Zero → both are equal

Top comments (0)