Java中对中文按字母顺序排序

使用JDK自带的Collator类,该类实现了Comparator接口,在Collator.getInstance()中可以指定特定的语言环境

    private void sortAttribute(List<Student> list){
        Comparator<Object> comparator = new Comparator<Object>() {
            @Override
            public int compare(Object o1, Object o2) {
                Student attribute1 = (Student) o1;
                Student attribute2 = (Student) o2;
                Collator collator = Collator.getInstance(java.util.Locale.CHINA);
                return collator.getCollationKey(attribute1.getName()).compareTo(collator.getCollationKey(attribute2.getName()));
            }
        };
        Collections.sort(list,comparator);
    }
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页