需求:需要针对集合进行排序,拥有字段a的对象排在前列,其他排在后面,字段a按大小排序,其余按字典序排序
Collections.sort(lm, new Comparator<Model>() {
@Override
public int compare(Model o1, Model o2) {A
if(o1.getA() != null && o2.getA() != null){
return Integer.valueOf(o1.getA()) - Integer.valueOf(o2.getA());
}else if(o1.getA() != null && o2.getA() == null){
return -1;
}else if(o1.getA() == null && o2.getA() != null){
return 1;
}else{
return o1.getName().compareTo(o2.getName());
}
}
});
本文介绍了一种集合排序的方法,首先确保拥有特定字段的对象排在前列,并按照该字段数值大小排序,对于不具有该字段的对象,则按照名称的字典序进行排序。这种排序策略能够有效地组织数据,便于后续的数据处理和展示。
1万+

被折叠的 条评论
为什么被折叠?



