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() + " ")); }
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/tech/pnotes/288344.html