Creating a Comparator using comparing method
suggest changeComparator.comparing(Person::getName)
This creates a comparator for the class Person that uses the person's name as the comparison key. comparingInt, comparingLong, and comparingDouble do the same for primitive keys. For example:
Comparator.comparingInt(Person::getAge)
Reversed order
To create a comparator that imposes the reverse ordering, use the reversed() method:
Comparator.comparing(Person::getName).reversed()
Chain of comparators
Comparator.comparing(Person::getLastName).thenComparing(Person::getFirstName)
This creates a comparator that first compares last names and then first names. You can chain as many comparators as you want.
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents