gfg
Comparable defines the natural ordering of a class's own objects by modifying the class itself, while Comparator defines custom, external sorting logic without altering the original class.
In Java, both Comparable and Comparator interfaces are used for sorting objects. The main difference between Comparable and Comparator is:
Comparable: It is used to define the natural ordering of the objects within the class.
Comparator: It is used to define custom sorting logic externally.
Comparable
Definition:It defines natural ordering within the class.
Method:compareTo()
Implementation:It is implemented in the class.
Sorting Criteria:Natural order sorting
Package:java.lang
Method:compareTo(T o)
Location:Implemented directly inside the target class.
Sorting Options:Provides exactly one default sorting sequence.
Modifies Class?:Yes, requires altering the class source code.
Invocation:Collections.sort(list)
Usage:It is used for a single sorting order.
Comparator
Definition:It defines external or custom sorting logic.
Implementation:It is implemented in a separate class.
Method:compare()
Sorting Criteria:Custom order sorting
Package:java.util
Method:compare(T o1, T o2)
Location:Implemented in separate classes or inline lambdas.
Sorting Options:Provides multiple dynamic sorting strategies.
Modifies Class?:No, works on classes you cannot modify.
Invocation:Collections.sort(list, comparator)
Usage:It is used for a multiple sorting order.
- Using Comparable (Natural Ordering) The target class implements Comparable and overrides compareTo. It takes one explicit argument to compare with this.
import java.util.*;
// Modifies the class itself
class Product implements Comparable<Product> {
private String name;
private int price;
public Product(String name, int price) {
this.name = name;
this.price = price;
}
public int getPrice() { return price; }
public String getName() { return name; }
// Natural sorting by price (ascending)
@Override
public int compareTo(Product other) {
return Integer.compare(this.price, other.price);
}
}
Usage:
List<Product> list = new ArrayList<>();
Collections.sort(list); // Automatically uses compareTo()
2. Using Comparator (Custom Ordering)
The sorting logic is separate from the entity. It takes two arguments to compare them against each other. It qualifies as a functional interface, meaning you can write it cleanly with Java 8+ lambdas.
// Option A: Traditional separate class approach
class NameComparator implements Comparator<Product> {
@Override
public int compare(Product p1, Product p2) {
return p1.getName().compareTo(p2.getName());
}
}
// Option B: Modern, cleaner Lambda or Method Reference approach
Comparator<Product> lambdaByName = (p1, p2) -> p1.getName().compareTo(p2.getName());
Comparator<Product> simpleByPrice = Comparator.comparing(Product::getPrice);
Usage
List<Product> list = new ArrayList<>();
// Pass both the list and your custom rule
Collections.sort(list, new NameComparator());
list.sort(Comparator.comparing(Product::getName));
Top comments (0)