java stream List 根据对象某个属性升序或降序排序
代码参考;
public class MyTests { @NoArgsConstructor @AllArgsConstructor @Data public static class DemoUser{ String name; Integer age; } public static void main(String[] args) { List<DemoUser> list = Arrays.asList( new DemoUser("张三", 12), new DemoUser("王五", 18), new DemoUser("王七", 18), new DemoUser("李四", 20) ); list.sort((o1, o2) -> { // return o1.getAge()-o2.getAge();//升序 return o2.getAge() - o1.getAge();//降序 }); System.out.println(JSON.toJSONString(list)); } }
搞定
https://www.leftso.com/article/231223140625636.html