其他分享
首页 > 其他分享> > Comparison method violates its general contract 排序报错

Comparison method violates its general contract 排序报错

作者:互联网

报错代码

list=list.sort{a,b->if(a.last_update<b.last_update) return -1 else return 1}

报错内容

java.lang.IllegalArgumentException: Comparison method violates its general contract!
at java.util.TimSort.mergeLo(TimSort.java:777)
at java.util.TimSort.mergeAt(TimSort.java:514)
at java.util.TimSort.mergeForceCollapse(TimSort.java:457)
at java.util.TimSort.sort(TimSort.java:254)
at java.util.Arrays.sort(Arrays.java:1512)
at java.util.ArrayList.sort(ArrayList.java:1460)
at java.util.Collections.sort(Collections.java:175)

解决方案

list=list.sort{a,b->if(a.last_update<b.last_update) return -1 else if(a.last_update<b.last_update) return 0 else return 1}

返回值会需要是 1 , -1, 0 这三个值

标签:sort,Comparison,java,TimSort,list,contract,util,报错
来源: https://www.cnblogs.com/overair/p/16415635.html