🌐
Oracle
docs.oracle.com › en › java › javase › 21 › docs › api › java.base › java › util › Comparator.html
Comparator (Java SE 21 & JDK 21)
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.
🌐
Oracle
docs.oracle.com › en › java › javase › 21 › docs › api › java.base › java › util › class-use › Comparator.html
Uses of Interface java.util.Comparator (Java SE 21 & JDK 21)
January 20, 2026 - Returns a lexicographic-order comparator with a function that extracts a long sort key. Methods in java.util with parameters of type Comparator
🌐
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
🌐
Inside.java
inside.java › 2023 › 02 › 21 › jepcafe17
Write Efficient Bug-free and Simple Comparators in Java - JEP Café #17 – Inside.java
dev.java&nbsp|&nbsp Newsletter&nbsp|&nbsp About&nbsp ... Comparator are elements used daily in all Java applications. There are fairly easy to write, but must also follow several subtle rules. This JEP Café explains all of them: how to leverage the factory methods from the Comparator interface, and from the wrapper classes of primitive types.
🌐
Coderanch
coderanch.com › t › 738448 › java › Comparator-comparing-reversed
Comparator - comparing() & reversed() (Beginning Java forum at Coderanch)
Comparator comparator = Comparator.comparing( (Test) x -> x.getNum() ).reversed();. Compile error output when I call chained Comparator reversed() on line #21.
🌐
W3Schools
w3schools.com › java › java_advanced_sorting.asp
Java Advanced Sorting (Comparator and Comparable)
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.
🌐
Oracle
docs.oracle.com › en › java › javase › 21 › docs › api › java.base › java › lang › Comparable.html
Comparable (Java SE 21 & JDK 21)
January 20, 2026 - For example, if one adds two keys a and b such that (!a.equals(b) && a.compareTo(b) == 0) to a sorted set that does not use an explicit comparator, the second add operation returns false (and the size of the sorted set does not increase) because a and b are equivalent from the sorted set's perspective. Virtually all Java core classes that implement Comparable have natural orderings that are consistent with equals.
Top answer
1 of 16
242

There are a couple of awkward things with your example class:

  • it's called People while it has a price and info (more something for objects, not people);
  • when naming a class as a plural of something, it suggests it is an abstraction of more than one thing.

Anyway, here's a demo of how to use a Comparator<T>:

public class ComparatorDemo {

    public static void main(String[] args) {
        List<Person> people = Arrays.asList(
                new Person("Joe", 24),
                new Person("Pete", 18),
                new Person("Chris", 21)
        );
        Collections.sort(people, new LexicographicComparator());
        System.out.println(people);
        Collections.sort(people, new AgeComparator());
        System.out.println(people);
    }
}

class LexicographicComparator implements Comparator<Person> {
    @Override
    public int compare(Person a, Person b) {
        return a.name.compareToIgnoreCase(b.name);
    }
}

class AgeComparator implements Comparator<Person> {
    @Override
    public int compare(Person a, Person b) {
        return a.age < b.age ? -1 : a.age == b.age ? 0 : 1;
    }
}

class Person {

    String name;
    int age;

    Person(String n, int a) {
        name = n;
        age = a;
    }

    @Override
    public String toString() {
        return String.format("{name=%s, age=%d}", name, age);
    }
}

EDIT

And an equivalent Java 8 demo would look like this:

public class ComparatorDemo {

    public static void main(String[] args) {
        List<Person> people = Arrays.asList(
                new Person("Joe", 24),
                new Person("Pete", 18),
                new Person("Chris", 21)
        );
        Collections.sort(people, (a, b) -> a.name.compareToIgnoreCase(b.name));
        System.out.println(people);
        Collections.sort(people, (a, b) -> a.age < b.age ? -1 : a.age == b.age ? 0 : 1);
        System.out.println(people);
    }
}
2 of 16
202

Here's a super short template to do the sorting right away :

Collections.sort(people, new Comparator<Person>() {
   @Override
   public int compare(final Person lhs, Person rhs) {
     // TODO return 1 if rhs should be before lhs 
     //      return -1 if lhs should be before rhs
     //      return 0 otherwise (meaning the order stays the same)
   }
 });

If it's hard to remember, try to just remember that it's similar (in terms of the sign of the number) to:

 lhs-rhs 

That's in case you want to sort in ascending order : from smallest number to largest number.

🌐
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.
Find elsewhere
🌐
Baeldung
baeldung.com › home › java › java streams › new stream, comparator and collector in guava 21
New Stream, Comparator and Collector in Guava 21 | Baeldung
January 8, 2024 - This API returns a new Comparator instance – which sorts in lexicographical (dictionary) order comparing corresponding elements pairwise. Internally, it creates a new instance of LexicographicalOrdering<S>(). MoreCollectors contains some very ...
🌐
Oracle
docs.oracle.com › en › java › javase › 21 › docs › api › java.base › java › util › SortedSet.html
SortedSet (Java SE 21 & JDK 21)
January 20, 2026 - This interface is a member of the Java Collections Framework. ... Returns the comparator used to order the elements in this set, or null if this set uses the natural ordering of its elements.
🌐
Javatpoint
javatpoint.com › Comparator-interface-in-collection-framework
Java Comparator - javatpoint
Java Comparator interface is used to order the user-defined class objects, compare() method, collection class, java comporator example, Example of Comparator interface in collection framework.
🌐
OpenJDK
cr.openjdk.org › ~pminborg › panama › 21 › v3 › javadoc › api › java.base › java › util › SortedMap.html
SortedMap (Java SE 21 [ad-hoc build])
This interface is a member of the Java Collections Framework. ... Returns the comparator used to order the keys in this map, or null if this map uses the natural ordering of its keys.
🌐
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   April 20, 2016
🌐
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 ...