按基元布尔类型对 ArrayList 进行排序
2022-09-01 10:05:12
我想使用布尔类型对我进行排序。基本上我想先显示条目。这是我下面的代码:ArrayList
true
abc.java
public class Abc {
int id;
bool isClickable;
Abc(int i, boolean isCl){
this.id = i;
this.isClickable = iCl;
}
}
主要.java
List<Abc> abc = new ArrayList<Abc>();
//add entries here
//now sort them
Collections.sort(abc, new Comparator<Abc>(){
@Override
public int compare(Abc abc1, Abc abc2){
boolean b1 = abc1.isClickable;
boolean b2 = abc2.isClickable;
if (b1 == !b2){
return 1;
}
if (!b1 == b2){
return -1;
}
return 0;
}
});
排序前的顺序:真真真假假假假假假
排序后的顺序:假假真真真假假假假