简体   繁体   English

使用自定义比较器排序列表不起作用

[英]Sorting list using custom comparator not working

I want to sort list of users on the basis of three criteria. 我想根据三个标准对用户列表进行排序。 So i have created custom comparator to sort list but its not working as i want 所以我创建了自定义比较器来排序列表,但它不能按我的意愿工作

public class CustomComparator implements Comparator {
    List<Integer> user1Time = new ArrayList<>();
    List<Integer> user2Time = new ArrayList<>();
    int user1Count, user2Count;
    ParseUser user1, user2;
    Date user1Date, user2Date;
    boolean user1Short, user2Short;
    private String TAG="CustomComparator";

    @Override
    public int compare(Object lhs, Object rhs) {
        user1 = (ParseUser) lhs;
        user2 = (ParseUser) rhs;


        user1Time = user1.getList("timeAvailable");
        user2Time = user2.getList("timeAvailable");


        //To compare Available time of both users with Searched Time

        if(user1Time!=null){
            user1Time.retainAll(Utils.availableTime);
            user1Count = user1Time.size();
        }else{
            user1Count=0;
        }

        if(user2Time!=null){
            user2Time.retainAll(Utils.availableTime);
            user2Count = user2Time.size();
        }else{
            user2Count=0;
        }



        Log.d(TAG, "compare: "+user1.getString("name")+" "+user1Count);
        Log.d(TAG, "compare: "+user2.getString("name")+" "+user2Count);



        //To compare lastSeen of both the users
        user1Date = user1.getDate("lastSeen");
        user2Date = user2.getDate("lastSeen");


        //To compare shortNotice avilablity of both the user
        user1Short = user1.getBoolean("shortNotice");
        user2Short = user2.getBoolean("shortNotice");

        if(user2Time!= null && user2Time!= null && user1Date!= null && user2Date!= null){
            if (user1Count>user2Count){
                if(user1Short){
                    if(user1Date.compareTo(user2Date)>0){
                        return -1;
                    }else {
                        return -1;
                    }
                }else if (user1Date.compareTo(user2Date)>0){
                    return -1;
                }else if (user2Date.compareTo(user1Date)>0){
                    return 1;
                }
            }else if(user2Short){
                if(user2Date.compareTo(user1Date)>0){
                    return 1;
                }else {
                    return -1;
                }
            }else if (user2Date.compareTo(user1Date)>0){
                return 1;
            }else if (user1Date.compareTo(user2Date)>0){
                return -1;
            }
        }
            return 0;
    }
}

but its not working properly 但它不能正常工作

i want to sort list in three ways array of integers,boolean and date as you can see in my code. 我想以三种方式对列表进行排序,整数数组,布尔值和日期,如我在代码中所见。

You could do something like this: 你可以这样做:

class Data {
int A;
int B;
int C;

public int getA() {
    return A;
}

public void setA(int a) {
    A = a;
}

public int getB() {
    return B;
}

public void setB(int b) {
    B = b;
}

public int getC() {
    return C;
}

public void setC(int c) {
    C = c;
}
}

class Sorter {
public void sort(List<Data> dataList){
    Collections.sort(dataList,
            Comparator.comparingInt(Data::getA)
            .thenComparingInt(Data::getB)
            .thenComparingInt(data->data.C)
    );
}
}

here i use only comparingInt but you could do it with any kind of comparators. 在这里我只使用comparingInt但你可以用任何类型的比较器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM