Is there a succinct way to create a Comparator<X> based on ordering by X.getY()

Viewed 38

Is there any shorter way to say this:

final Comparator<ClassA> byName = 
    (final ClassA a1, final ClassA a2) 
        -> a1.getName().compareTo( a2.getName() ));

I know that getName() will never return null.

Perhaps something along these lines using a method reference:

final Comparator<ClassA> byName = ????( ClassA::getName );
1 Answers
Related