创建测试类:

public class Student implements Comparable {private int age;private String nameEn;private int grade;public Student(int age, String nameEn, int grade) {this.age = age;this.nameEn = nameEn;this.grade = grade;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}public String getNameEn() {return nameEn;}public void setNameEn(String nameEn) {this.nameEn = nameEn;}public int getGrade() {return grade;}public void setGrade(int grade) {this.grade = grade;}public static int compare(Student s1, Student s2) {return Integer.compare(s1.getAge(), s2.getAge());}@Overridepublic int compareTo(Student s) {return Integer.compare(this.getAge(), s.getAge());}}/** * 初始化数据 * * @return */public static List initData() {List list = new ArrayList();list.add(new Student(11, "zhangsan", 5));list.add(new Student(12, "lisi", 5));list.add(new Student(9, "wangwu", 6));list.add(new Student(13, "zhaoliu", 6));list.add(new Student(8, "tianqi", 3));list.add(new Student(6, "wangba", 1));list.add(new Student(8, "jiujiu", 2));return list;}

传统排序方式

1.在 Java 8 之前,对集合进行排序要为 Comparator 创建一个匿名内部类用来排序,比如,按照 age 字段从小到大排序:

/** * 传统匿名内部类比较器 */ @Test public void soreted1() { List list = Java8SortedDemo.initData(); list.sort(new Comparator() { @Override public int compare(Student s1, Student s2) { return Integer.compare(s1.getAge(), s2.getAge()); } }); Assert.assertEquals(list.get(0).getNameEn(), "wangba"); } 

2.或者让 Student 实现 Comparable 接口,复写 compareTo 方法,同样按照 age 字段从小到大排序:

@Override public int compareTo(Student s) { return Integer.compare(this.getAge(), s.getAge()); } /** * 实现Comparable接口 */ @Test public void soreted2() { List list = Java8SortedDemo.initData(); Collections.sort(list); Assert.assertEquals(list.get(0).getNameEn(), "wangba"); } 

java8 排序方式

3.在 java8 中引入了重要的特性之一 lambda,根据 Lambda 表达式的介绍,我们现在可以不使用匿名内部类,只使用简单实用的语义就可以得到相同的结果,按照 age 字段从小到大排序:

/** * java8的Lambda,显式指定比较类型 */ @Test public void soreted3() {List list = Java8SortedDemo.initData();list.sort((Student s1, Student s2) -> Integer.compare(s1.getAge(), s2.getAge()));Assert.assertEquals(list.get(0).getNameEn(), "wangba"); } 

4.我们甚至可以省略类型,让编译器自行推测,按照 age 字段从小到大排序:

/** * java8的Lambda,不指定比较类型 */ @Test public void soreted4() { List list = Java8SortedDemo.initData(); list.sort((s1, s2) -> Integer.compare(s1.getAge(), s2.getAge())); Assert.assertEquals(list.get(0).getNameEn(), "wangba"); } 

5.我们还可以使用静态方法排序,按照 age 字段从小到大排序:

public static int compare(Student s1, Student s2) { return Integer.compare(s1.getAge(), s2.getAge()); } /** * 自定义静态方法 */ @Test public void soreted5() { List list = Java8SortedDemo.initData(); list.sort(Student::compare); Assert.assertEquals(list.get(0).getNameEn(), "wangba");}

6.当然,java8 为我们准备好了通用的 Comparator.comparing 方法,按照 age 字段从小到大排序:

/** * Comparator提供的静态方法 */ @Test public void soreted6() { List list = Java8SortedDemo.initData(); list.sort(Comparator.comparing(Student::getAge)); Assert.assertEquals(list.get(0).getNameEn(), "wangba"); } 

java8 排序方法拓展:

java8 同样让我们具备编写更复杂逻辑排序的方法表达式,

7.比如,反转排序,按照 age 字段从大到小排序:

/** * 反转排序 */ @Test public void soreted7() { List list = Java8SortedDemo.initData(); list.sort(Comparator.comparing(Student::getAge).reversed()); Assert.assertEquals(list.get(0).getNameEn(), "zhaoliu"); } 

8.比如,多字段组合排序,先按照 grade 从大到小,相同则按照 age 从大到小排序:

/** * 多字段组合排序(匿名类方式) */ @Test public void soreted8() { List list = Java8SortedDemo.initData(); list.sort((s1, s2) -> { if (Integer.compare(s1.getGrade(), s2.getGrade()) != 0) { return Integer.compare(s2.getGrade(), s1.getGrade()); } return Integer.compare(s2.getAge(), s1.getAge()); }); Assert.assertEquals(list.get(0).getNameEn(), "zhaoliu"); } 

9.java8 的 Comparator 为我们准备了更便利的链式风格组合排序方式,先按照 grade 从大到小,相同则按照 age 从小到大排序:

 /** * 多字段组合排序(链式接口) */@Testpublic void soreted9() {List list = Java8SortedDemo.initData();list.sort(Comparator.comparing(Student::getGrade).reversed().thenComparing(Student::getAge));Assert.assertEquals(list.get(0).getNameEn(), "wangwu");}