Hackforge Academy

Category: React • Beginner

Published on 18 Feb 2026

Explanation

Comparable is an interface present in the java.lang package. It is used to define the natural ordering of objects.

Code Example

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

Explanation

Comparable is used when you want to sort objects of a class based on a single default sorting logic such as id, name, or salary.

Code Example

// Used for default sorting
Collections.sort(list);

Explanation

The compareTo() method is used to compare the current object with another object. It returns negative, zero, or positive value.

Code Example

public int compareTo(Student s) {
    return this.id - s.id;
}

Explanation

If compareTo() returns negative, current object is smaller. If zero, both are equal. If positive, current object is greater.

Code Example

// Negative  -> this < other
// Zero      -> this == other
// Positive  -> this > other

Explanation

Here is an example where Student class implements Comparable to sort by id.

Code Example


Explanation

Code Example

import java.util.*;
class Student implements Comparable<Student> {
    int id;
    String name;
    Student(int id, String name) {
        this.id = id;
        this.name = name;
    }
    public int compareTo(Student s) {
        return this.id - s.id;
    }
}

Explanation

Once a class implements Comparable, objects can be sorted using Collections.sort().

Code Example

List<Student> list = new ArrayList<>();
Collections.sort(list);

Explanation

Comparable defines natural ordering. For example, String and Integer classes already implement Comparable.

Code Example

String a = "Apple";
String b = "Banana";
System.out.println(a.compareTo(b));

Explanation

Comparable allows only one sorting logic because compareTo() method is defined inside the class.

Code Example

// Only one default sorting possible
class Student implements Comparable<Student> {
    public int compareTo(Student s) {
        return this.id - s.id;
    }
}

Explanation

Comparable is an interface present in the java.lang package. It is used to define the natural ordering of objects.

Code Example

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

Explanation

Comparable is used when you want to sort objects of a class based on a single default sorting logic such as id, name, or salary.

Code Example

// Used for default sorting
Collections.sort(list);

Explanation

The compareTo() method is used to compare the current object with another object. It returns negative, zero, or positive value.

Code Example

public int compareTo(Student s) {
    return this.id - s.id;
}

Explanation

If compareTo() returns negative, current object is smaller. If zero, both are equal. If positive, current object is greater.

Code Example

// Negative  -> this < other
// Zero      -> this == other
// Positive  -> this > other

Explanation

Here is an example where Student class implements Comparable to sort by id.

Code Example


Explanation

Code Example

import java.util.*;
class Student implements Comparable<Student> {
    int id;
    String name;
    Student(int id, String name) {
        this.id = id;
        this.name = name;
    }
    public int compareTo(Student s) {
        return this.id - s.id;
    }
}

Explanation

Once a class implements Comparable, objects can be sorted using Collections.sort().

Code Example

List<Student> list = new ArrayList<>();
Collections.sort(list);

Explanation

Comparable defines natural ordering. For example, String and Integer classes already implement Comparable.

Code Example

String a = "Apple";
String b = "Banana";
System.out.println(a.compareTo(b));

Explanation

Comparable allows only one sorting logic because compareTo() method is defined inside the class.

Code Example

// Only one default sorting possible
class Student implements Comparable<Student> {
    public int compareTo(Student s) {
        return this.id - s.id;
    }
}

Want structured learning with real projects?

Join our Weekend Live Workshop and become job-ready faster.