Comparable Interface
- In Java, the Comparable interface may be a effective tool that gives a way to compare objects of a class.
- When a class actualizes the Comparable interface, it permits its instances to be compared to one another, empowering sorting and ordering of collections like arrays, lists, or other data structures.
- The Comparable interface is basic once you need to characterize a natural ordering for the objects of a class.

Definition of Comparable Interface
The Comparable interface is part of the java.lang package, and it announces a single method:
public int compareTo(T o);
- T:The type of the object that’s being compared.
- compareTo(T o):This method compares the current object (this) with the desired protest (o) of sort T. It returns:
1.A negative integer if the current object is less than the required object.
2.Zero on the off chance that the current object is equal to the required object.
3.A positive numbers in case the current object is more prominent than the desired object.
The compareTo method gives a implies to execute a natural ordering, which is utilized by different classes, such as Collections.sort() and Arrays.sort(), to order objects in a collection.
Example of Comparable Interface
Let’s consider a Person class where we need to sort people by their age. To attain this, the Person class must actualize the Comparable interface.
class Person implements Comparable<Person> {
private String name;
private int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public int getAge() {
return age;
}
public String getName() {
return name;
}
@Override
public int compareTo(Person other) {
// Compare by age
return Integer.compare(this.age, other.age);
}
@Override
public String toString() {
return name + " (" + age + " years old)";
}
}
Here, the Person class actualizes the Comparable interface, and the compareTo method compares Person objects based on their age. This permits occurrences of Person to be compared and sorted normally by age.
Sorting with Comparable
Presently, let’s see how able to utilize Comparable to sort a list of Person objects.
import java.util.*;
public class Main {
public static void main(String[] args) {
List<Person> people = new ArrayList<>();
people.add(new Person("Alice", 30));
people.add(new Person("Bob", 25));
people.add(new Person("Charlie", 35));
// Sorting the list using Comparable
Collections.sort(people);
// Printing the sorted list
for (Person p : people) {
System.out.println(p);
}
}
}
Output:
Bob (25 years old)
Alice (30 years old)
Charlie (35 years old)
- As you’ll be able see, the Person objects are sorted by age, much appreciated to the compareTo method within the Person class.
Types of Comparable Implementations
There are a few ways to utilize the Comparable interface based on the business logic of the application:
1.Normal Ordering:
- Usually the default comparison logic for a class that implements Comparable.
- The class characterizes how objects of that sort ought to be ordered by implementing compareTo().
- Example:
Sorting a list of Worker objects based on their compensation or sorting a list of Book objects based on their title
2.Custom Ordering:
- In case you need to characterize numerous ways to compare objects, you’ll actualize the Comparable interface and give distinctive compareTo() implementations in several classes.
- Then again, you’ll be able utilize external sorting logic by means of Comparator (described below).
3.Numeric Comparisons:
- For numeric types, the compareTo method frequently uses the Integer.compare() or Double.compare() methods to handle the comparison cleanly.
4.String Comparisons:
- For string comparison, String class implements Comparable, where compareTo is implemented lexicographically (lexicon order).
- Example:
Sorting a list of String values.
Comparator vs Comparable
- Whereas the Comparable interface is valuable for characterizing the common ordering of objects, some of the time you might require different ways to compare objects.
- In such cases, the Comparator interface comes in convenient.
- It permits you to characterize custom sorting logic autonomously of the objects themselves.
- Comparable is for a characteristic ordering defined inside the class.
- Comparator is utilized for outside custom sorting that can be connected on objects of the same class but with diverse comparison procedures.