Guava provides this functionality through its awesome Ordering . Ordering - Comparator ++. In this case, if you have a list of some type that Comparable implements, you can write:
boolean sorted = Ordering.natural().isOrdered(list);
This works for any Iterable , not just List , and you can easily handle null by specifying whether they should appear before or after any other null elements:
Ordering.natural().nullsLast().isOrdered(list);
In addition, since you mentioned that you want to check the reverse order as well as the normal one, this will be done as:
Ordering.natural().reverse().isOrdered(list);
Java 8 users . Instead, use the equivalent Comparators#isInOrder(Iterable) as the rest of the order is mostly out of date (as explained in the class documentation ).
ColinD Jun 15 '10 at 16:33 2010-06-15 16:33
source share