1.对象类型排列
  List<Person> list = Arrays.asList(
    new Person(22, "shaomch", "man"),
    new Person(26, "mike", "wemon"),
    new Person(24, "tom", "wemon"),
    new Person(22, "tom", "wemon")
  );
  //升序排列
  List<Person> list1 = list.stream().sorted(Comparator.comparing(Person::getAge)).collect(Collectors.toList());
  //降序排列
  List<Person> list2 = list.stream().sorted(Comparator.comparing(Person::getAge).reversed()).collect(Collectors.toList());


2.Integer类型排列
  List<Integer> integerList = Arrays.asList(1,4,56,2,3,5,10);
  //升序
  List<Integer> integerList1 = integerList.stream().sorted(Comparator.naturalOrder()).collect(Collectors.toList());
  //降序
  List<Integer> integerList2 = integerList.stream().sorted(Comparator.reverseOrder()).collect(Collectors.toList());


3.String类型排列
  List<String> strList = Arrays.asList("shaomch", "mike", "tom", "tom");
  //升序
  List<String> strList1 = strList.stream().sorted(Comparator.naturalOrder()).collect(Collectors.toList());
  //降序
  List<String> strList2 = strList.stream().sorted(Comparator.reverseOrder()).collect(Collectors.toList());

相关文章:

  • 2022-01-11
  • 2021-08-19
猜你喜欢
  • 2021-06-17
  • 2022-01-14
  • 2022-12-23
  • 2022-01-23
  • 2022-12-23
  • 2021-11-11
  • 2020-07-19
相关资源
相似解决方案