1、自然序排序一个list:
list.stream().sorted().collect(Collectors.toList());
2、自然序逆序元素,使用Comparator
提供的reverseOrder()
方法:
list.stream().sorted(Comparator.reverseOrder()) .collect(Collectors.toList());
3、按某个字段排序List:
list.stream().sorted(Comparator.comparing(Student::getAge)) .collect(Collectors.toList());
4、按某个字段倒序排序List:
list.stream().sorted(Comparator.comparing(Student::getAge).reversed()) .collect(Collectors.toList());
备注:Student是实体,age是属性名