# Stream的排序

1、list<Integer>的正序

List<Integer> list = new ArrayList<>();
List<Integer> collect = list.stream().sorted().collect(Collectors.toList());
System.out.println("list<Integer>元素正序：" + collect);

list<Integer>元素正序：[25, 32, 45, 50, 98]

2、list<Integer>的倒序

List<Integer> list = new ArrayList<>();
List<Integer> collect = list.stream().sorted(Comparator.reverseOrder()).collect(Collectors.toList());
System.out.println("list<Integer>元素倒序：" + collect);

list<Integer>元素倒序：[98, 50, 45, 45, 32]

3、List<Person>的元素的属性值正序

Person p1 = new Person("张三", new BigDecimal("50.0"));
Person p2 = new Person("王五", new BigDecimal("25.0"));
Person p3 = new Person("李四", new BigDecimal("68.0"));
Person p4 = new Person("李四", new BigDecimal("17.0"));
Person p5 = new Person("张三", new BigDecimal("45.0"));
List<Person> list = new ArrayList<>();
list.sort(Comparator.comparing(person -> person.getSalary()));
System.out.println(list);

4、List<Person>的元素的属性值倒序

Person p1 = new Person("张三", new BigDecimal("50.0"));
Person p2 = new Person("王五", new BigDecimal("25.0"));
Person p3 = new Person("李四", new BigDecimal("68.0"));
Person p4 = new Person("李四", new BigDecimal("17.0"));
Person p5 = new Person("张三", new BigDecimal("45.0"));
List<Person> list = new ArrayList<>();
List<Person> collect = list.stream().sorted(Comparator.comparing(Person::getSalary).reversed()).collect(Collectors.toList());
System.out.println("元素的属性值倒序：" + collect);

Stream的排序

(0)
(0)