iam*_*nos 1 java sorting list assertj
我正在使用AssertJ进行测试,并且注意到有一种检查a List<T>是否排序的方法:
public static <T> void sorted(final List<T> actual) {
try {
assertThat(actual).isSorted();
} catch (AssertionError e) {
LOGGER.error(e.getMessage(), e);
throw e;
}
}
Run Code Online (Sandbox Code Playgroud)
有没有办法检查列表是否按降序排列?
我知道番石榴提供了,Ordering.natural().reverse().isOrdered(values)但是我想利用AssertJ的assert消息,因为它确实对调试很有帮助,例如
group is not sorted because element 5:
<"4000366190001391">
is not less or equal than element 6:
<"4000206280001394">
group was:
<["4000206280001363",
"4000206280001364",
"4000206280001365",
"4000206280001373",
"4000206280001388",
"4000366190001391",
"4000206280001394",
"4000366190001401",
"4000206280001403",
"4000206280001405",
....]>
Run Code Online (Sandbox Code Playgroud)
是。还有一种方法isSortedAccordingTo需要一个Comparator。
您需要将泛型类型参数更改为<T extends Comparable<T>>其他值,否则无法确定顺序。
public static <T extends Comparable<T>> void sorted(final List<T> actual) {
try {
assertThat(actual).isSortedAccordingTo(Comparator.reverseOrder());
} catch (AssertionError e) {
LOGGER.error(e.getMessage(), e);
throw e;
}
}
Run Code Online (Sandbox Code Playgroud)