赞
踩
1.写个测试类
- @Data
- @NoArgsConstructor
- public class Test {
-
- private String id;
- private String name;
- private String age;
- private String score;
- }
2.做测试
- public static void main(String[] args) {
-
- List<Test> list=new ArrayList<>();
- list.add(new Test("zs",12,97.2));
- list.add(new Test("ls",8,62.3));
- list.add(new Test("ww",13,99.9));
-
-
- System.out.println("一开始的排序顺序");
- System.out.println(list);
-
- System.out.println(" 按照age 升序");
- list.sort(Comparator.comparing(Test::getAge)); //核心代码
- System.out.println(list);
-
- System.out.println("按照score 降序");
- list.sort(Comparator.comparing(Test::getScore).reversed()); //核心代码
- System.out.println(list);
-
- }
-
-
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。