Search code examples
javaandroidcollator

Wrong sorting with Collator using Locale.TRADITIONAL_CHINESE


I want to order the list by Chinese text strokes count and I am using the method to create Collator to achieve it but it seems the Collator with TRADITIONAL_CHINESE have some error.

for the example,

 List<String> strList = new List<>();
 strList.add("日");
 strList.add("蘋");
 Collections.sort(strList, new SortChinese());




 class SortChinese implements Comparator<String> {

        public int compare(String obj1, String obj2) {

            Collator collator = Collator.getInstance(Locale.TRADITIONAL_CHINESE);
            collator.setStrength(Collator.PRIMARY);
            return collator.compare(obj1, obj2);

        }
    }

before: 日,蘋

after: 蘋,日

expected result : 日,蘋

Any idea or suggestion for Chinese strokes count Sorting in android?


Solution

  • You can remove ShortChinese:

    List<String> strList = new List<>();
    strList.add("日");
    strList.add("蘋"); 
    
    Collator collator = Collator.getInstance(Locale.TRADITIONAL_CHINESE);
    collator.setStrength(Collator.PRIMARY);
    Collections.sort(strList, collator);