1. 程式人生 > >java/android 對列表進行首字母排序和過濾相同字串

java/android 對列表進行首字母排序和過濾相同字串

List<String> topicNames = new ArrayList<>(new HashSet<>(topicName));
if (!topicNames.isEmpty()) {    //不為空

    Collections.sort(topicNames, new Comparator<String>() {

        @Override

        public int compare(String str1,
                           String str2) {

            //根據文字排序

            return str1.compareTo(str2);
        }

    });
}

結果是:英文名稱的檔案排序正常,中文名稱的排序並沒有按照首字母順序。

另一個方法:

  1. Comparator cmp= Collator.getInstance(Locale.CHINA);

  2. Collections.sort(mFilename1,cmp);

過濾相同字串:

List<String> topicNames = new ArrayList<>(new HashSet<>(topicName));

新增new HashSet();