比较器错误:比较方法违反了其一般合同

时间:2018-04-19 08:23:19

标签: java android json retrofit2 comparator

我想通过Comparator通过一个float参数对我的json进行排序。但它告诉我这个错误: 比较方法违反了其总契约!

我有另一个正常工作的Comparator类,但其中一个是Comparating另一个东西

这是我的比较者:

 public class MyChangeComparator implements Comparator<myContact> {

int type = 1;

public MyChangeComparator(int type) {

    this.type = type;

}


@Override
public int compare(myContact myContact, myContact t1) {

    if (myContact.getPercent_change_1h() == null || t1.getPercent_change_1h() == null
            || myContact.getPercent_change_24h() == null || t1.getPercent_change_24h() == null ||
            myContact.getPercent_change_7d() == null || t1.getPercent_change_7d() == null){
        return 0;
    }
    float m1 = 1;
    float m2 = 2;

    boolean ba = false;
    boolean aa = false;

    if (type == 1){
        String a = myContact.getPercent_change_1h();
        if (a.contains("-")){
            a = a.replace("-" , "");
            aa = true;
        }
        m1 = Float.parseFloat(a);
        if (aa == true){
            m1 = -m1;
            aa = false;
        }
        String b = t1.getPercent_change_1h();
        if (b.contains("-")){
            b = b.replace("-" , "");
            ba = true;
        }
        m2 = Float.parseFloat(b);
        if (ba == true){
            m2 = -m2;
            ba = false;
        }
    }else if (type == 2){
        String a = myContact.getPercent_change_24h();
        a = a.replace("-" , "");
        m1 = Float.parseFloat(a);
        String b = t1.getPercent_change_24h();
        b = b.replace("-" , "");
        m2 = Float.parseFloat(b);
    }else if (type == 3) {
        String a = myContact.getPercent_change_7d();
        a = a.replace("-" , "");
        m1 = Float.parseFloat(a);
        String b = t1.getPercent_change_7d();
        b = b.replace("-" , "");
        m2 = Float.parseFloat(b);
    }


    if (m1 < m2){
        return 1;
    }else if (m1 >= m2){
        return -1;
    }else {
        return 0;
    }
}

应用程序在我更改返回时正在运行:

if (m1 < m2){
    return -1;
}else if (m1 >= m2){
    return 1;
}else {
    return 0;
}

我认为当我使用return 1应用程序崩溃时

0 个答案:

没有答案