🌐
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.
🌐
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.
🌐
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.
Find elsewhere
🌐
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.
🌐
Jenkov
jenkov.com › tutorials › java-collections › comparator.html
Java Comparator
October 4, 2020 - The Java Comparator interface, java.util.Comparator, represents a component that can compare two objects so they can be sorted using sorting functionality in Java. When sorting e.g a Java List you can pass a Java Comparator to the sorting method.
🌐
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.
🌐
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 …
🌐
DEV Community
dev.to › sohailshah › different-ways-to-use-comparator-interface-in-java-4mgj
Different Ways To Use Comparator Interface in Java - DEV Community
July 15, 2023 - One way to use the Comparator interface is by creating a separate class that implements the Comparator interface. This class will provide the logic for comparing objects based on specific criteria. You can then instantiate this class and use it to sort collections of objects.
🌐
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:
🌐
Medium
medium.com › @AlexanderObregon › javas-comparator-comparing-method-explained-342361288af6
Java’s Comparator.comparing() Method Explained | Medium
October 2, 2024 - Java’s Comparator.comparing() method is a powerful tool that simplifies custom sorting based on object properties. By allowing us to create comparators for sorting collections like lists, this method reduces the complexity of implementing ...
🌐
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.