javajava-8guavacomparatorlexicographic-ordering

What is the equivalent way to do Ordering.lexicographical() in Java 8 Comparator?


Is there a way to implement Ordering.lexicographical() with Java 8 Comparator?

Comparator.thenCompare seems to be limited in this


Solution

  • Seemingly not, no.

    As a result, Guava still provides this functionality, but in the new class Comparators: https://guava.dev/releases/snapshot/api/docs/com/google/common/collect/Comparators.html#lexicographical(java.util.Comparator).

    Note that Guava generally does a good job telling you whether and how to migrate off of it to new Java features, so you're often better off checking its Javadoc than digging on your own. In this case: https://guava.dev/releases/snapshot/api/docs/com/google/common/collect/Ordering.html#lexicographical().