Oracle
docs.oracle.com › javase › 8 › docs › api › java › util › Comparator.html
Comparator (Java Platform SE 8 )
October 20, 2025 - A comparison function, which imposes a total ordering on some collection of objects. Comparators can be passed to a sort method (such as Collections.sort or Arrays.sort) to allow precise control over the sort order.
Reddit
reddit.com › r/java › noob programmer, eli5: comparators?
r/java on Reddit: Noob Programmer, ELI5: Comparators?
October 23, 2014 -
I know that there is http://docs.oracle.com/javase/7/docs/api/java/util/Comparator.html but I'm genuinely confused about them and would appreciate any english translation if at all possible.
I know that it contains the compare(blah, blah) method but how does that work?
Top answer 1 of 3
14
You can implement the Comparator method on a class in order to enable alternative sorting or searching methods through the java library. For instance, if you wanted to sort an ArrayList of type String based on String length, then you could write a class called StringLengthComparator that implements Comparator with the desired comparison. StringLengthComparator slc = new StringLengthComparator(); int result = slc.compare(a, b); If a is shorter than b, then the method would return a number less than zero. If it is longer than b, then it would return a number greater than zero. If they are the same length, then it would return zero. You could then sort the ArrayList using Collections.sort. Collections.sort(myArrayList, slc);
2 of 3
10
Coding time! Now let's assume we have a person class like this: public class Person { private String firstName = null; private String lastName = null; public Person(String firstName, String lastName) { this.firstName = firstName; this.lastName = lastName; } public String getFirstName() { return this.firstName; } public String getLastName() { return this.lastName; } } And let's assume we want to store that person alphabetically by its last name. So we could write an comparator like this: import java.util.Comparator; public class PersonComparator implements Comparator { @Override public int compare(Person p1, Person p2) { return p1.getLastName().compareTo(p2.getLastName()); } @Override public boolean equals(Object obj) { return obj.equals(this); } } Finally we will store this in a collection of type ArrayList like this: ArrayList addressBook = new ArrayList<>(); addressBook.add(new Person("Christopher", "Nolan")); addressBook.add(new Person("JJ", "Abrams")); addressBook.add(new Person("David", "Lynch")); addressBook.add(new Person("Tim", "Burton")); Now if we list the address book with: for (Person p : addressBook) { System.out.println(p.getLastName() + ", " + p.getFirstName()); } we'll get an unsorted output: Nolan, Christopher Abrams, JJ Lynch, David Burton, Tim But luckily collections do come with a sorting mechanism. All we have to do is providing the logic for comparing two items: addressBook.sort(new PersonComparator()); and we get a sorted output: Abrams, JJ Burton, Tim Lynch, David Nolan, Christopher Now with Java 8 you even can skip the step of implementing a Comparator and simply use a lamda expression like this: addressBook.sort((Person p1, Person p2) -> p1.getLastName().compareTo(p2.getLastName())); Full example here . Note that there is a subreddit ( r/javahelp ) for questions like these! Happy coding.
Videos
06:11
Comparator Interface in Java - Tutorial for Beginners | Learn ...
15:43
#95 Comparator vs Comparable in Java - YouTube
04:25
How to use Comparator in Java - YouTube
08:25
Comparable Interface vs Comparator Interface | Java Tutorial - YouTube
23:41
Java Comparable and Comparator and easy sorting - YouTube
Java Comparator Made EASY! Learn Object Sorting Like a Pro
Reddit
reddit.com › r/learnprogramming › comparator vs comparable
r/learnprogramming on Reddit: Comparator vs Comparable
March 22, 2017 -
Hi Everyone :) can someone please explain to me what is the difference between Comparator and Comparable ? I mean when should i use one or another? Can i think of it this way : the Comparable's compareTo() defines the standard way of sorting a list (using the 1 arg sort() in Collections) and if i want to compare objects in many different ways i add Comparators to the class and i call the 2 args sort() ? Thanks in advance Melodie
Top answer 1 of 3
2
Yeah, I think you understand it. If you're writing a class whose instances have some intrinsic/obvious way of being compared, then you might want to have that class implement Comparable. If you need to sort a list/array of objects that are not Comparable, or if you want to sort them in some custom manner, then you would want to use a Comparator object to specify how the objects should be compared.
2 of 3
2
Comparable provides a standard way to sort objects, whereas Comparator is used to sort objects against the normal sorting defined in the compareTo method on that object type.
GitHub
github.com › frohoff › jdk8u-jdk › blob › master › src › share › classes › java › util › Comparator.java
jdk8u-jdk/src/share/classes/java/util/Comparator.java at master · frohoff/jdk8u-jdk
import java.util.Comparators; · /** * A comparison function, which imposes a <i>total ordering</i> on some · * collection of objects. Comparators can be passed to a sort method (such · * as {@link Collections#sort(List,Comparator) Collections.sort} or {@link ·
Author frohoff
Tutorialspoint
tutorialspoint.com › home › java › java comparator example
Java Comparator Example
September 1, 2008 - Discover how to effectively use the Java Comparator interface for sorting and comparison in Java programming.
GeeksforGeeks
geeksforgeeks.org › java › java-comparator-interface
Java Comparator Interface - GeeksforGeeks
The Comparator interface in Java is used to define custom sorting logic for objects. It belongs to java.util package allows sorting of objects of user-defined classes without modifying their source code.
Published 1 month ago
DZone
dzone.com › coding › java › java 8 comparator: how to sort a list
Java 8 Comparator: How to Sort a List
May 20, 2019 - We’ve written London with a lowercase "L" to better highlight differences between Comparator.naturalOrder(), which returns a Comparator that sorts by placing capital letters first, and String.CASE_INSENSITIVE_ORDER, which returns a case-insensitive Comparator. Basically, in Java 7, we were using Collections.sort() that was accepting a List and, eventually, a Comparator – in Java 8 we have the new List.sort(), which accepts a Comparator.
Codecademy
codecademy.com › docs › java › comparator
Java | Comparator | Codecademy
July 28, 2025 - Comparator in Java is an interface used to order objects of an arbitrary class. It is not to be confused with the Comparable interface, which is implemented by the class to be sorted.
GeeksforGeeks
geeksforgeeks.org › java › stream-sorted-comparator-comparator-method-java
Stream sorted (Comparator comparator) method in Java - GeeksforGeeks
December 26, 2025 - In Java, the Stream.sorted(Comparator comparator) method sorts the elements of a stream based on a provided Comparator and returns a new sorted stream.
Mkyong
mkyong.com › home › java8 › java 8 lambda : comparator example
Java 8 Lambda : Comparator example - Mkyong.com
August 5, 2015 - In this example, we will show you how to use Java 8 Lambda expression to write a Comparator to sort a List.
Android Developers
developer.android.com › api reference › comparator
Comparator | API reference | Android Developers
Skip to main content · English · Deutsch · Español – América Latina · Français · Indonesia · Polski · Português – Brasil · Tiếng Việt · 中文 – 简体
Coderanch
coderanch.com › t › 560667 › java › Understanding-Comparator
Understanding Comparator (Java in General forum at Coderanch)
December 4, 2011 - The code that would be calling compare() of the Comparator would either get a positive integer, zero or a negative integer. But how does it know what order(ascending/descending) it has to sort in? See the documentation of the Comparable.compareTo() method in Java API doc.
GeeksforGeeks
geeksforgeeks.org › java › comparator-comparingdouble-method-in-java-with-examples
Comparator comparingDouble() method in Java with examples - GeeksforGeeks
July 11, 2025 - static <T> Comparator<T> comparingDouble( ToDoubleFunction <T> keyExtractor) Parameters: This method accepts a single parameter keyExtractor which is the function used to extract the double sort key. Return value: This method returns a comparator that compares by an extracted key Exception: This method throws NullPointerException if the argument is null. Below programs illustrate comparingDouble(java.util.function.ToDoubleFunction) method: Program 1:
Baeldung
baeldung.com › home › java › core java › comparator and comparable in java
Comparator and Comparable in Java | Baeldung
March 26, 2025 - A quick and practical guide to Comparable and Comparator interfaces.
W3Schools
w3schools.com › java › java_advanced_sorting.asp
Java Advanced Sorting (Comparator and Comparable)
Java Examples Java Videos Java Compiler Java Exercises Java Quiz Java Code Challenges Java Server Java Syllabus Java Study Plan Java Interview Q&A Java Certificate ... In the List Sorting Chapter, you learned how to sort lists alphabetically and numerically, but what if the list has objects in it? To sort objects you need to specify a rule that decides how objects should be sorted. For example, if you have a list of cars you might want to sort them by year, the rule could be that cars with an earlier year go first. The Comparator and Comparable interfaces allow you to specify what rule is used to sort objects.