Null safe date comparator for sorting in Java 8 Stream

If it’s the Items that may be null, use @rgettman’s solution.

If it’s the LocalDates that may be null, use this:

items.stream()
     .sorted(Comparator.comparing(Item::getCreateDt, Comparator.nullsLast(Comparator.reverseOrder())));

In either case, note that sorted().findFirst() is likely to be inefficient as most standard implementations sort the entire stream first. You should use Stream.min instead.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)