首页 > 代码库 > jdk7 中Collections.sort 异常
jdk7 中Collections.sort 异常
Collections.sort 异常
java.lang.IllegalArgumentException: Comparison method violates its general contract! at java.util.TimSort.mergeHi(TimSort.java:868) at java.util.TimSort.mergeAt(TimSort.java:485) at java.util.TimSort.mergeForceCollapse(TimSort.java:426) at java.util.TimSort.sort(TimSort.java:223) at java.util.TimSort.sort(TimSort.java:173) at java.util.Arrays.sort(Arrays.java:659) at java.util.Collections.sort(Collections.java:217)
代码如下:
Collections.sort(values, new Comparator<Map.Entry<String, Float>>() { <span style="white-space:pre"> </span>public int compare(Map.Entry<String, Float> o1, Map.Entry<String, Float> o2) { <span style="white-space:pre"> </span>return (o2.getValue() > o1.getValue() ? 1 : -1); } });
Collections.sort(values, new Comparator<Map.Entry<String, Float>>() { public int compare(Map.Entry<String, Float> o1, Map.Entry<String, Float> o2) { return o2.getValue().compareTo(o1.getValue()); } });
异常解决,参考链接:
http://stackoverflow.com/questions/10234038/compare-method-throw-exception-comparison-method-violates-its-general-contract
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。