1、准备测试数据
@Data @NoArgsConstructor @AllArgsConstructor public class User { private int id; private int age; private String name; private BigDecimal money; }
private static List<User> getList() { List<User> users = new ArrayList<>(); u(new User(1, 28, "张三", new BigDecimal(4000))); u(new User(2, 21, "李四", new BigDecimal(5000))); u(new User(3, 24, "王五", new BigDecimal(6000))); u(new User(4, 23, "赵六", new BigDecimal(7000))); return users; }
2、升序
List<User> ascUsersByAge = getList().stream().sorted(User::getAge)).collec());
3、降序
List<User> descUsersByAge = getList().stream().sorted(User::getAge).reversed()).collec());
4、组合排序
List<User> orderUsersByAgeAndMoney = getList().stream().sorted(User::getAge).thenComparing(User::getMoney)).collec());
路漫漫其修远兮,吾将上下而求索
译文:在追寻真理方面,前方的道路还很漫长,但我将百折不挠,不遗余力地去追求和探索。
如果您有什么好的想法与方法,欢迎评论区留言,我们一起讨论~