Creating a Comparator using comparing method

suggest change
Comparator.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.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents