1.按单个字段排序

  1. list.stream().sorted(Comparator.comparing(User::getAge)).collect(Collectors.toList());

2.按多个字段排序

  1. //age升序
  2. Comparator byIdASC = Comparator.comparing(Student::getAge);
  3. //named不分区大小写降序
  4. Comparator byNameDESC = Comparator.comparing(Student::getName,String.CASE_INSENSITIVE_ORDER).reversed();
  5. //birthday 降序
  6. Comparator byBirthdayDESC = Comparator.comparing(Student::getBirthday).reversed();
  7. //联合排序
  8. Comparator finalComparator = byIdASC.thenComparing(byNameDESC).thenComparing(byBirthdayDESC);
  9. List<Student> result = stuList.stream().sorted(finalComparator).collect(Collectors.toList());