How to determine if a List is sorted in Java?
Guava provides this functionality through its Comparators class.
boolean sorted = Comparators.isInOrder(list, comparator);
There's also the Ordering class, though this is mostly obsolete. An Ordering
is a Comparator
++. In this case, if you have a list of some type that implements Comparable
, you could write:
boolean sorted = Ordering.natural().isOrdered(list);
This works for any Iterable
, not just List
, and you can handle null
s easily by specifying whether they should come before or after any other non-null
elements:
Ordering.natural().nullsLast().isOrdered(list);
Also, since you mentioned that you'd like to be able to check for reverse order as well as normal, that would be done as:
Ordering.natural().reverse().isOrdered(list);
Stream
If you are using Java 8 or later, streams may be able to help.
list.stream().sorted().collect(Collectors.toList()).equals(list);
More briefly, in Java 16+, using Stream#toList
.
list.stream().sorted().toList().equals(list);
This code will sort the list out of place and collect its elements in another list, which is then compared to the initial list. The comparison will be successful, if both lists contain the same elements in equal positions.
Note that this method will have worse space and time complexity than other approaches because it will have to sort the list out of place, so it should not be used for very large lists. But it is the easiest to use because it is a single expression and does not involve 3rd party libraries.
Here's a generic method that will do the trick:
public static <T extends Comparable<? super T>>
boolean isSorted(Iterable<T> iterable) {
Iterator<T> iter = iterable.iterator();
if (!iter.hasNext()) {
return true;
}
T t = iter.next();
while (iter.hasNext()) {
T t2 = iter.next();
if (t.compareTo(t2) > 0) {
return false;
}
t = t2;
}
return true;
}