簡體   English   中英

使用自定義比較器排序列表不起作用

[英]Sorting list using custom comparator not working

我想根據三個標准對用戶列表進行排序。 所以我創建了自定義比較器來排序列表,但它不能按我的意願工作

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;
    }
}

但它不能正常工作

我想以三種方式對列表進行排序,整數數組,布爾值和日期,如我在代碼中所見。

你可以這樣做:

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)
    );
}
}

在這里我只使用comparingInt但你可以用任何類型的比較器。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM