Java8按某个字段排序
作者:互联网
public void sorted() { EmployDO emp = EmployDO.builder().age("18").name("张小华").sex("男").position("服务员").build(); EmployDO emp2 = EmployDO.builder().age("20").name("张松月").sex("女").position("服务员").build(); EmployDO emp3 = EmployDO.builder().age("30").name("李桂芝").sex("女").position("服务员").build(); EmployDO emp4 = EmployDO.builder().age("26").name("宋倩").sex("女").position("服务员").build(); EmployDO emp5 = EmployDO.builder().age("12").name("徐盛").sex("男").position("服务员").build(); List<EmployDO> employDOList = Arrays.asList(emp, emp2, emp3, emp4, emp5); // 升序 employDOList.stream().sorted(Comparator.comparing(EmployDO::getAge)).collect(Collectors.toList()) .forEach(s -> System.out.print(s.getName() + " ")); System.out.println(" "); // 降序 employDOList.stream().sorted(Comparator.comparing(EmployDO::getAge).reversed()) .forEach(s -> System.out.print(s.getName() + " ")); }
标签:name,builder,sex,build,某个,position,排序,Java8,EmployDO 来源: https://www.cnblogs.com/JYB2021/p/16670822.html