简体   繁体   中英

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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