Say I have a class with a potentially-null field and I want to sort a List of class instances on that field first (nulls last), then fall back on a second non-null field.
@Data
class Foo {
String bar;
int baz
}
Given a list of foos called foos
and static imports from java.util.Comparator
:
foos.stream()
.sorted(comparing(Foo::getBar, nullsLast(String::compareTo)).thenComparing(Foo::getBaz))