实现中文首字母排序

    @Test
    public void test1() {
        //实现中文首字母排序
        List<Dto> list = Arrays.asList(new Dto("鲍勃", 16), new Dto("艾琳", 18), new Dto("蔡依林", 19));
        Comparator comparator = Collator.getInstance(Locale.CHINA);
        Collections.sort(list, (o1, o2) -> comparator.compare(o1.getName(), o2.getName()));
        System.out.println(list);
        //[TestMain.Dto(name=艾琳, age=18), TestMain.Dto(name=鲍勃, age=16), TestMain.Dto(name=蔡依林, age=19)]
    }

    @Data
    @AllArgsConstructor
    class Dto {
        private String name;
        private Integer age;
    }

posted @ 2022-05-08 21:07  进击的小蔡鸟  阅读(28)  评论(0编辑  收藏  举报