Hackforge Academy

Category: java

Comparator

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:

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:

// 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:

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:

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

Explanation

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

Code:


Explanation


Code:

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:

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

Explanation

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

Code:

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:

// 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:

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:

// 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:

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:

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

Explanation

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

Code:


Explanation


Code:

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:

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

Explanation

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

Code:

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:

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

πŸš€ Learn Spring Boot with real-world projects

πŸ’‘ Build REST APIs step by step

🧠 Improve backend development skills

🎯 Get career-ready practical training

Join Our Free WhatsApp Community

Direct access to niche-specific mentors and peers on WhatsApp.

🐍

Python Community

Discuss Django, FastAPI, AI integration, and automation scripts with 15k+ developers.

Join Python Community
βš›οΈ

React Community

Master Next.js, Framer Motion, and State Management. Share your latest UI components.

Join React Community
β˜•

Java Community

Deep dives into Spring Boot, Microservices architecture, and high-performance backend ops.

Join Java Community