java学习之sorted排序
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());