list.add(new Student(3, "b", 1, new Date(11110000)));
list.add(new Student(1, "c", 3, new Date(44440000)));
list.add(new Student(2, "a", 2, new Date(22210000)));
list.add(new Student(4, "a", 11, new Date(33330000)));
System.out.println("-------原来序列-------------------");
for (Student stu : list) {
System.out.println(stu.toString());
}
// 按age正序排序,注意结果排完后是1,2,3,11. 不是1,11,2,3(如果是String类型正序排序是这样)
SortListUtil.sort(list, "age", null);
System.out.println("---------测试Integer和正序,按age正序排序-----------------");
for (Student stu : list) {
System.out.println(stu.toString());
}
// 按id倒序
SortListUtil.sort(list, "id", SortListUtil.DESC);
System.out.println("--------测试int和倒序,按id倒序------------------");
for (Student stu : list) {
System.out.println(stu.toString());
}
// 先按name正序排序,再按id正序排序
SortListUtil.sort(list, new String[] { "name", "id" }, new String[] {});
System.out
.println("---------测试多个排序字段,先按name正序,name相同时再按id正序-----------------");
for (Student stu : list) {
System.out.println(stu.toString());
}
// 先按name正序排序,再按id倒序排序
SortListUtil.sort(list, new String[] { "name", "id" }, new String[] {
SortListUtil.ASC, SortListUtil.DESC });
System.out
.println("---------测试多个排序字段,先按name正序,name相同时再按id倒序-----------------");
for (Student stu : list) {
System.out.println(stu.toString());
}
// 按birthday排序
SortListUtil.sort(list, "birthday");
System.out
.println("---------测试实现了Comparable接口的对象排序,按birthday正序-----------------");
for (Student stu : list) {
System.out.println(stu.toString());
}
// sortByMethod
SortListUtil.sortByMethod(list, "getId", null);
System.out
.println("---------测试sortByMethod,按getId方法正序-----------------");
for (Student stu : list) {
System.out.println(stu.toString());
}
}
}
测试执行效果:
推荐阅读: