集合:按元素的中文屬性排序

1. 要排序的元素類:

 public static class NameCount implements Comparable<NameCount> {

        Collator collator = Collator.getInstance(java.util.Locale.CHINA);

        @ApiModelProperty(value = "名")
        private String name;
        @ApiModelProperty(value = "次數")
        private Integer count;

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public Integer getCount() {
            return count;
        }

        public void setCount(Integer count) {
            this.count = count;
        }

        @Override
        public int compareTo(NameCount o) {
            return collator.compare(this.name, o.getName());
        }
    }

2. 集合:

List<NameCount> NameCountList = Lists.newArrayList();

該集合中有多個元素後,按name排序的實現:

 Collections.sort(NameCountList);

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章