我知道这个问题可能有一些非常好的解释,但我仍然无法将它应用到我的代码中。在我的情况下如何避免该错误?这是我的代码:
sampleList.sort((o1, o2) -> {
try {
if (o1.getDate() == null || o2.getDate() == null) return 0;
int i = new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy").parse(o1.getDate()).compareTo(new SimpleDateFormat("EEE MMM dd HH:mm:ss z yyyy").parse(o2.getDate()));
return i;
} catch (ParseException e) {
logger.error("Sorting failed", e);
}
return 0;
});
sampleList 是一个类型的列表,其中包含字符串中的日期。这是我得到的一个例外:
java.lang.IllegalArgumentException: Comparison method violates its general contract!
at java.util.TimSort.mergeLo(Unknown Source)
at java.util.TimSort.mergeAt(Unknown Source)
at java.util.TimSort.mergeCollapse(Unknown Source)
at java.util.TimSort.sort(Unknown Source)
at java.util.Arrays.sort(Unknown Source)
at java.util.ArrayList.sort(Unknown Source)
at
如果您对如何解决它有任何建议,请与我分享。谢谢你。