Use Comparable if you want to define a default (natural) ordering behaviour of the object in question, a common practice is to use a technical or natural (database?) identifier of the object for this.

Use Comparator if you want to define an external controllable ordering behaviour, this can override the default ordering behaviour.

See also:

  • Sorting an ArrayList of objects using a custom sorting order
Answer from BalusC on Stack Overflow
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ java โ€บ comparable-vs-comparator-in-java
Java Comparable vs Comparator - GeeksforGeeks
August 18, 2025 - Functional Interface were introduced in Java 8. It has exactly one abstract method. In Comparator<T>, the only abstract method is: int compare(T o1, T o2);
๐ŸŒ
W3Schools
w3schools.com โ€บ java โ€บ java_advanced_sorting.asp
Java Advanced Sorting (Comparator and Comparable)
It is easier to use the Comparable interface when possible, but the Comparator interface is more powerful because it allows you to sort any kind of object even if you cannot change its code. ... If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail: sales@w3schools.com ยท If you want to report an error, or if you want to make a suggestion, send us an e-mail: help@w3schools.com ยท HTML Tutorial CSS Tutorial JavaScript Tutorial How To Tutorial SQL Tutorial Python Tutorial W3.CSS Tutorial Bootstrap Tutorial PHP Tutorial Java Tutorial C++ Tutorial jQuery Tutorial
People also ask

What is the most crucial difference between Comparable and Comparator in Java?
Comparable defines a single natural object sorting order by implementing compareTo() within the class. The Comparator allows for multiple sorting orders, implemented externally via the compare() method.
๐ŸŒ
theknowledgeacademy.com
theknowledgeacademy.com โ€บ blog โ€บ comparable-vs-comparator
Comparable vs. Comparator: Key Differences and When to Use
Can a class implement both Comparable and Comparator?
A class can implement Comparable to provide natural ordering, while a Comparator can define alternate sorting strategies without altering the original class logic.
๐ŸŒ
theknowledgeacademy.com
theknowledgeacademy.com โ€บ blog โ€บ comparable-vs-comparator
Comparable vs. Comparator: Key Differences and When to Use
What are Related Courses and Blogs Provided by The Knowledge Academy?
The Knowledge Academy offers various Java Courses, including the Java Programming Course, JavaScript for Beginners Course and Hibernate Training. These courses cater to different skill levels, providing comprehensive insights into Super Keyword in Java. Our Programming &amp; DevOps Blogs cover a range of topics related to Java, offering valuable resources, best practices, and industry insights. Whether you are a beginner or looking to advance your Programming Skills, The Knowledge Academy's diverse courses and informative blogs have got you covered.
๐ŸŒ
theknowledgeacademy.com
theknowledgeacademy.com โ€บ blog โ€บ comparable-vs-comparator
Comparable vs. Comparator: Key Differences and When to Use
๐ŸŒ
DigitalOcean
digitalocean.com โ€บ community โ€บ tutorials โ€บ comparable-and-comparator-in-java-example
Comparable and Comparator in Java Example | DigitalOcean
August 3, 2022 - Comparator interface compare(Object o1, Object o2) method need to be implemented that takes two Object argument, it should be implemented in such a way that it returns negative int if the first argument is less than the second one and returns zero if they are equal and positive int if the first argument is greater than the second one. Comparable and Comparator interfaces use Generics for compile-time type checking, learn more about Java Generics.
๐ŸŒ
The Knowledge Academy
theknowledgeacademy.com โ€บ blog โ€บ comparable-vs-comparator
Comparable vs. Comparator: Key Differences and When to Use
In Java, both the Comparable and Comparator interfaces are used to compare objects for sorting, but they operate differently. Comparable provides a natural ordering for objects by implementing the compareTo() method within the class itself.
Find elsewhere
Top answer
1 of 7
10

Comparable interface

The Comparable interface defines a type's natural ordering. Suppose you have a list of String or Integer objects; you can pass that list to

Collections.sort(list);

and you will have a sorted list. How? Because String and Integer both implement Comparable interface and the implementations of Comparable interface provide a natural ordering. Its like the class definition saying - "If you find a collection of objects of my type, order them according to the strategy I have defined in the compareTo method".

Now when you define your own type, you can define the natural ordering of the objects of your class by implementing the Comparable interface. See the Java documentation for more information on object ordering.

Comparator interface

The Comparator interface describes how to define custom strategies for object ordering. Suppose we have a simple Person type as below:

public class Person {
    String name;

    public Person(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }
}

Now, by implementing the Comparator interface, you can write different strategies to order the instances of your Person type. For example, consider the two strategies for ordering Person objects given below:

class StrategyOne implements Comparator<Person> {

    @Override
    public int compare(Person p1, Person p2) {
        return p1.getName().length() - p2.getName().length();
    }

}

class StrategyTwo implements Comparator<Person> {

    @Override
    public int compare(Person p1, Person p2) {
        return p1.getName().compareTo(p2.getName());
    }

}

Here, StrategyOne will order Person objects based on the length of their names, and StrategyTwo will order Person objects based on lexicographic ordering of their names.

The ways to implement Comparator

As you can see, the concrete strategy classes are stateless, hence all instances are functionally equivalent. So, we just need a single instance of any concrete strategy class. Thus, it should be a singleton. Using anonymous classes will create a new instance each time the call is executed. Consider storing the object in a private static final field and reusing it by using static factory methods to access them [Effective Java]. For example, you can reuse the above two concrete strategies as below:

class Strategies {
    private static final Comparator<Person> PERSON_NAME_LENGTH_COMPARATOR = new StrategyOne();

    private static final Comparator<Person> PERSON_NAME_LEXICAL_COMPARATOR = new StrategyTwo();

    public static Comparator<Person> personNameLengthComparator(){
         return  PERSON_NAME_LENGTH_COMPARATOR;
    }


    public static Comparator<Person> personNameLexicalComparator(){
         return  PERSON_NAME_LEXICAL_COMPARATOR;
    }
}

Summary

To summarize, the Comparable interface is used to define the natural ordering of a class, and the Comparator interface is used to define particular strategies for object ordering.

2 of 7
8

In what way is a comparator superior to comparable?

It is not "superior". It is just that the two interfaces are doing (roughly) the same thing in different ways. In the Comparable case the ordering logic is in the object being ordered. In the Comparator case, the logic is in a different class from the objects being declared.

But I don't see a reason why I should use both for sorting employee objects

The only case where it would make sense to use both would be if you needed to be able to sort the objects into different orders. Then you could declare the relevant classes as implementing Comparable for the "natural" order and use Comparator objects to implement the other orders.

By the way, a comparator probably should not implement Comparable, and vice versa.

If a comparator implements Comparable that implies you are trying to order instances of the comparator object itself ...

Your PersonComparator class is misnamed. It should really be called Person.


Could you clarify one thing in your answer that we have already equals() method from Object class then why the Comparator interface is facilitating the equals() method again?

A number of points:

  • You still seem to be confusing the purpose of Comparable and Comparator. The equals method on a Comparator object compares the comparator with other comparators!!

  • The equals method tells you whether two objects are equal ... not which one comes first.

  • The reason that Comparator overrides equals is solely so that they can clearly document what equals(Object) does when you call it on a Comparator object. (The actual behaviour is entirely consistent with Object.equals(Object) ... but they obviously thought it necessary to do this because programmers were repeatedly getting the semantics of the method wrong.)

๐ŸŒ
Oracle
docs.oracle.com โ€บ javase โ€บ 8 โ€บ docs โ€บ api โ€บ java โ€บ util โ€บ Comparator.html
Comparator (Java Platform SE 8 )
October 20, 2025 - Unlike Comparable, a comparator may optionally permit comparison of null arguments, while maintaining the requirements for an equivalence relation. This interface is a member of the Java Collections Framework.
๐ŸŒ
Medium
ashutoshkrris.medium.com โ€บ comparable-vs-comparator-explained-in-java-0aabaedf8d47
Comparable vs Comparator Explained in Java | by Ashutosh Krishna | Medium
July 21, 2024 - The Comparator interface in Java provides a way to define multiple ways to compare and sort objects. Unlike the Comparable interface, which allows only a single natural ordering, Comparator is designed to offer flexibility by allowing multiple sorting strategies...
๐ŸŒ
Scaler
scaler.com โ€บ home โ€บ topics โ€บ java โ€บ comparable and comparator in java
Difference between Comparable and Comparator in Java - Scaler Topics
July 13, 2023 - Comparable and Comparator in Java allow us to define custom sorting behavior for objects, including sorting based on multiple data members.
๐ŸŒ
LabEx
labex.io โ€บ tutorials โ€บ java-comparator-and-comparable-117394
Mastering Java Comparator and Comparable | LabEx
This complete application demonstrates the power and flexibility of the Comparable and Comparator interfaces in Java. It allows you to sort objects in various ways to meet different requirements of your application. In this lab, you learned how to compare and sort objects in Java using the Comparable and Comparator interfaces.
๐ŸŒ
Medium
medium.com โ€บ @ganesh.shah โ€บ comparator-vs-comparable-java-8-940a83f53bd3
Comparator vs Comparable | Java 8 | by GANESH SHAH | Medium
March 8, 2024 - Comparator vs Comparable | Java 8 In Java, Comparator and Comparable are interfaces used for sorting objects, but they serve different purposes: Comparable Interface: The Comparable interface is โ€ฆ
๐ŸŒ
Medium
medium.com โ€บ @pratik.941 โ€บ understanding-comparable-and-comparator-interface-in-java-their-role-in-sorting-4338b3017fa9
Understanding Comparable and Comparator interface in Java: Their Role in Sorting | by Pratik T | Medium
October 4, 2024 - The compare() method compares two objects and returns: โ€” A negative integer if the first argument is less than the second. โ€” Zero if the first argument is equal to the second.
๐ŸŒ
HappyCoders.eu
happycoders.eu โ€บ java โ€บ comparator-comparable-compareto
compareTo, Comparable, Comparator - Comparing Objects in Java
June 12, 2025 - You compare Java primitives (int, long, double, etc.) using the operators <, <=, ==, =>, >.
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ difference-between-comparable-and-comparator-in-java
Difference between Comparable and Comparator in Java
Comparable and Comparator both are an interface that can be used to sort the elements of the collection. Comparator interface belongs to java.util package while comparable belongs to java.lang package. Comparator interface sort collection using two o
๐ŸŒ
Coderanch
coderanch.com โ€บ t โ€บ 677942 โ€บ java โ€บ Comparator-Comparable
Comparator or Comparable (Java API forum at Coderanch)
In general implement comparable for natural order, (natural order definition is obviously open to interpretation), and write a comparator for other sorting or comparison needs. I suppose you go with comparator. refer https://www.javacodegeeks.com/2013/03/difference-between-comparator-and-comparable-in-java.html
๐ŸŒ
Upgrad
upgrad.com โ€บ home โ€บ tutorials โ€บ software & tech โ€บ comparable and comparator in java
Comparable vs Comparator in Java Guide
May 19, 2025 - It defines a comparison function for sorting objects. Unlike Comparable, Comparator works independently of the class being compared. This allows for multiple sorting criteria without modifying the original class.
๐ŸŒ
Blogger
javarevisited.blogspot.com โ€บ 2023 โ€บ 04 โ€บ 7-examples-of-comparator-and-comparable.html
7 Examples of Comparator and Comparable in Java 8
The Comparable interface is used to compare the objects of the same class and sort them based on their natural order. In this article, we'll explore 7 different examples of Comparator and Comparable in Java 8. We'll see how we can use them to ...