1、基础介绍
sorted():自然排序
@Test
public void test1(){
List<String> list = Arrays.asList("ccc","bbb","aaa");
list.stream().sorted().forEach(System.out::println);
}
sorted(Comparator com):自定义排序
List<Student> students = Arrays.asList(
new Student(1,"张三",60),
new Student(2,"李四",70),
new Student(3,"王五",80),
new Student(4,"赵六",90),
new Student(4,"赵六",90),
new Student(4,"赵六",90),
new Student(5,"田七",100));
@Test
public void test2(){
students.stream().sorted((x,y) -> {
if(x.getName().equals(y.getName())){
return x.getScore().compareTo(x.getScore());
}else{
return x.getScore().compareTo(y.getScore());
}
}).forEach(System.out::println);
}