对象
public class Student{
private Long id;
private String name;
private int age;
private int score;
}
需排序的学生对象list
List<Student> studentList = new ArrayList<>();
排序规则设置
Comparator mycmp = ComparableComparator.getInstance();
//默认为正序,我们这边设置为逆序
mycmp = ComparatorUtils.reversedComparator(mycmp);
//允许对象的属性值为空时排在后面(倒序时)
mycmp = ComparatorUtils.nullHighComparator(mycmp);
声明要排序的对象的属性,并指明所使用的排序规则,如果不指明,则用默认排序
ArrayList<Object> sortFields = new ArrayList<Object>();
sortFields.add( new BeanComparator("score" , mycmp)); //主排序(第一排序)
sortFields.add( new BeanComparator("age" , mycmp)); //次排序(第二排序)
创建一个排序链
ComparatorChain multiSort = new ComparatorChain(sortFields);
开始真正的排序,按照先主,后副的规则
Collections.sort (studentList , multiSort);
所需依赖
commons-beanutils-1.8.3.jar
commons-collections-3.2.1.jar
commnos-logging-1.0.4.jar