Accepts a function that extracts a Comparable
sort key from a type T
, and returns a
Comparator<T>
that compares by that sort key.
The returned comparator is serializable if the specified function is also serializable.
<T> | the type of element to be compared | |
<U> | the type of the Comparable sort key | |
keyExtractor | the function used to extract the Comparable sort key |
NullPointerException | if the argument is null |
@apiNote
For example, to obtain a Comparator
that compares
Person
objects by their last name,
Comparator<Person> byLastName = Comparator.comparing(Person::getLastName);
Diagram: Comparator