首页 > 代码库 > java8 stream sorted

java8 stream sorted

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());

java8 stream sorted