C/C++教程

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 这三个值

这篇关于Comparison method violates its general contract 排序报错的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!