Skip to content
Advertisement

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

Advertisement

Answer

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().

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement