对list按某一字段进行排序
方法一:collection.sort(list,comparator),传入一个排序器
//1.MenuList排序;Comparator<SecurityConfigMenus> pcCompare = new Comparator<SecurityConfigMenus>() {
@Override
public int compare(SecurityConfigMenus o1,SecurityConfigMenus o2) {
if (o1.getSerialNo() != null) {
if (o2.getSerialNo() != null) {
return (int) (Integer.parseInt(o1.getSerialNo()) -Integer.parseInt(o2.getSerialNo()));
} else {
return 1;
}
} else {
if (o2.getSerialNo() != null) {
return -1;
} else {
return 0;
}
}
}
};
Collections.sort(newWholeList, pcCompare);
方法二:list中类实现comparable接口,重写compareTo方法,然后list直接添加元素即可。