【发布时间】:2016-12-01 06:10:13
【问题描述】:
我想根据三个标准对用户列表进行排序。 所以我创建了自定义比较器来排序列表,但它没有按我想要的方式工作
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定义不是working properly。请包括MCVE -
使用
thenComparing -
“不工作”是什么意思?能详细点吗?
-
我的意思是它对列表进行排序,但有时即使他的 shortNotice 是真的,有更多时间可用性的用户也不会排在首位。我希望用户具有更好的三分之二或全部三个标准。如果时间可用性列表很小,但如果列表扩展超过 10 个项目,则它不起作用
-
@YassinHajaj 请给我一些 thenComparing 的例子