如何测试列表是否使用 AssertJ 排序

How can I test whether a list is sorted with AssertJ

给出

List<Integer> list = ...

我想用AssertJ测试它是否排序。类似于:

assertThat(list).isSorted()

可能吗?

import com.google.common.collect.Ordering;

assertTrue(Ordering.natural().isOrdered(list));

AssertJ 提供开箱即用的 isSorted() and isSortedAccordingTo(Comparator) 列表断言:

List<String> strings = List.of("abc", "DEF");

assertThat(strings).isSorted(); // fails as "abc" is after "DEF" lexicographically
assertThat(strings).isSortedAccordingTo(String.CASE_INSENSITIVE_ORDER); // succeeds