Java 错误解决之java.lang.IllegalArgumentException: Comparison method violates its general contract

在jdk1.7上使用分类排序会报下面错误

java.lang.IllegalArgumentException: Comparison method violates its general contract!
    at java.util.TimSort.mergeLo(TimSort.java:743)
    at java.util.TimSort.mergeAt(TimSort.java:479)
    at java.util.TimSort.mergeCollapse(TimSort.java:406)
    at java.util.TimSort.sort(TimSort.java:210)
    at java.util.TimSort.sort(TimSort.java:169)
    at java.util.Arrays.sort(Arrays.java:2038)
    at java.util.Collections.sort(Collections.java:1891)

测试了很多网上说的基本都不行,最后加了个判断排序两个相等返回0就可以了,

public int compare(FileInfo file1, FileInfo file2) {

            if (file1.getFirstLetter()==file2.getFirstLetter()){
                return 0;
            }else {
                if (file1.getFirstLetter().equals("#")) {
                    return 1;
                } else if (file2.getFirstLetter().equals("#")) {
                    return -1;
                } else {
                    //return comp(file1.getFirstLetter(), file2.getFirstLetter());
                    return file1.getFirstLetter().compareTo(file2.getFirstLetter());
                }
            }
    }

具体原因的话是因为1.7的新算法抛的异常,这里有详细解释: http://www.cnblogs.com/JAYIT/p/6170426.html

  this.minGallop = var11 < 1?1:var11;
                    if(var2 == 1) {
                        assert var4 > 0;

                        System.arraycopy(var5, var8, var5, var9, var4);
                        var5[var9 + var4] = var6[var7];
                    } else {
                        if(var2 == 0) {
                            throw new IllegalArgumentException("Comparison method violates its general contract!");
                        }

                        assert var4 == 0;

                        assert var2 > 1;

                        System.arraycopy(var6, var7, var5, var9, var2);
                    }


你可能感兴趣的:(android)