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 - In Comparator<T>, the only abstract ... so it is not abstract in the interface. Therefore, Comparator qualifies as a functional interface and can be used with lambdas in Java 8+....
๐ŸŒ
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 โ€ฆ
๐ŸŒ
Baeldung
baeldung.com โ€บ home โ€บ java โ€บ core java โ€บ comparator and comparable in java
Comparator and Comparable in Java | Baeldung
March 26, 2025 - Java 8 provides new ways of defining Comparators by using lambda expressions, and the comparing() static factory method.
๐ŸŒ
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 ...
๐ŸŒ
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
๐ŸŒ
Oracle
docs.oracle.com โ€บ javase โ€บ 8 โ€บ docs โ€บ api โ€บ java โ€บ util โ€บ Comparator.html
Comparator (Java Platform SE 8 )
October 20, 2025 - Suppose a sorted set (or sorted map) with an explicit comparator c is used with elements (or keys) drawn from a set S. If the ordering imposed by c on S is inconsistent with equals, the sorted set (or sorted map) will behave "strangely." In particular the sorted set (or sorted map) will violate the general contract for set (or map), which is defined in terms of equals. For example, suppose one adds two elements a and b such that (a.equals(b) && c.compare(a, b) != 0) to an empty TreeSet with comparator c.
๐ŸŒ
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.
Find elsewhere
๐ŸŒ
Readthedocs
java-8-tips.readthedocs.io โ€บ en โ€บ stable โ€บ comparator.html
7. Comparator โ€” Java 8 tips 1.0 documentation
... This method uses the given key extracting function that applies on T type elements to generate U type comparable sort keys. To compare two elements of type T, it first applies the key extracting function to both the elements and then performs the sorting operation on the resulted keys.
๐ŸŒ
Java67
java67.com โ€บ 2019 โ€บ 06 โ€บ top-5-sorting-examples-of-comparator-and-comparable-in-java.html
6 Advanced Comparator and Comparable Examples in Java 8 | Java67
Learn Java and Programming through articles, code examples, and tutorials for developers of all levels. ... The JDK 8 release has completely changed the way you compare objects and sort them in Java. The new features of the Java 8 language e.g. lambda expression and method reference have made it easier to implement both Comparator and Comparable interface, as you don't need an Anonymous class for inline implementation.
๐ŸŒ
Javatpoint
javatpoint.com โ€บ difference-between-comparable-and-comparator
Comparable vs Comparator - javatpoint
Java Java is used to order the objects of a user-defined class. This interface is found in java.util package and contains 2 methods compare(Object obj1,Object obj2) and equals(Object element).
๐ŸŒ
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. โ€” A positive integer if the first argument is greater than the second. ... import java.util.Comparator; public class Student { private String name; private int rollNumber; public Student(String name, int rollNumber) { this.name = name; this.rollNumber = rollNumber; } public String getName() { return name; } public int getRollNumber() { return rollNumber; } @Override public String toString() { return "Student{name='
๐ŸŒ
Blogger
javarevisited.blogspot.com โ€บ 2021 โ€บ 09 โ€บ comparator-comparing-thenComparing-example-java-.html
Java 8 Comparator comparing() and thenComparing() Example - Tutorial
These methods accept a key extractor function and return a Comparator that can compare to that key. The key must be Comparable though like String, Integer, or any Java class which implements java.lang.Comparable interface, I mean the key must implement Comparable interface.
๐ŸŒ
Medium
ashutoshkrris.medium.com โ€บ comparable-vs-comparator-explained-in-java-0aabaedf8d47
Comparable vs Comparator Explained in Java | by Ashutosh Krishna | Medium
July 21, 2024 - To define multiple ways of comparing objects, we can use the Comparator interface, which we will explore in the next section. The Comparator interface in Java provides a way to define multiple ways to compare and sort objects.
๐ŸŒ
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-c...
๐ŸŒ
Upgrad
upgrad.com โ€บ home โ€บ tutorials โ€บ software & tech โ€บ comparable and comparator in java
Comparable vs Comparator in Java Guide
May 19, 2025 - These interfaces provide powerful tools for custom object sorting. Comparable offers simple natural ordering through direct class implementation. Comparator delivers flexible sorting options through separate implementation.
๐ŸŒ
DEV Community
dev.to โ€บ tlylt โ€บ comparator-comparable-oop-java-8-ko
Comparator & Comparable - [OOP & Java #8] - DEV Community
February 17, 2022 - Have an external function and pass it two items and ask it to compare based on some conditions. Have items themselves know their order. Then, ask the object directly if it is "larger" or "smaller" than another object.
๐ŸŒ
Readthedocs
java8tips.readthedocs.io โ€บ en โ€บ stable โ€บ comparator.html
7. Comparator โ€” Java 8 tips 1.0 documentation
... This method uses the given key extracting function that applies on T type elements to generate U type comparable sort keys. To compare two elements of type T, it first applies the key extracting function to both the elements and then performs the sorting operation on the resulted keys.
๐ŸŒ
Baeldung
baeldung.com โ€บ home โ€บ java โ€บ guide to java comparator.comparing()
Guide to Java Comparator.comparing() | Baeldung
January 8, 2024 - A practical guide to the static functions and instance methods of the Comparable interface that were introduced in Java 8.
๐ŸŒ
LabEx
labex.io โ€บ tutorials โ€บ java-comparator-and-comparable-117394
Mastering Java Comparator and Comparable | LabEx
Unsorted Student List: John, GPA: 3.5, Reg No: 101 Mary, GPA: 3.8, Reg No: 102 Alice, GPA: 3.2, Reg No: 103 Bob, GPA: 3.9, Reg No: 104 Students sorted by name (using Comparator): Alice, GPA: 3.2, Reg No: 103 Bob, GPA: 3.9, Reg No: 104 John, GPA: 3.5, Reg No: 101 Mary, GPA: 3.8, Reg No: 102 ยท As you can see, the students are now sorted alphabetically by name, rather than by GPA. Java 8 introduced lambda expressions, which can simplify creating comparators.