Reputation: 33
I have a list with string, which some contains some chinese characters:
List<String> list = new ArrayList<String>();
list.add("安卓");
list.add("iPhone");
list.add("WindowsMobile");
list.add("苹果");
list.add("Ubuntu");
list.add("Windows7");
How do I sort it?
Upvotes: 3
Views: 1940
Reputation:
You can do it with Collator.getInstance(Locale.CHINESE)
,the code:
Collections.sort(yourlist, new Comparator<String>() {
@Override
public int compare(Stringa1, Stringa2) {
Collator collator = Collator.getInstance(Locale.CHINESE);
int mySort = collator.compare(a1, a2);
return mySort;
}
});
Upvotes: 0
Reputation: 9182
Collections.sort using your own comparator would definitely be a start. To create a comparator, have a class implement Comparator
and implement its compare method. Then, instantiate an object and pass it to the Collections.sort method.
EDIT:
Approx psuedocode for the logic inside the compare method:
IF s1 isChinese && s2 isEnglish
return -1
IF s2 isChinese && s1 isEnglish
return 1
IF s1 isChinese && s2 isChinese
//implement your custom way to sort stuff
IF s1 isEnglish && s2 isEnglish
//implement your custom way to sort stuff
This way, your chinese words will appear at the top instead of at the bottom.
Upvotes: 1
Reputation: 13501
A better way is to translate it to english (see here) and then sort it using Comparator..
Upvotes: 0
Reputation: 43504
Use the normal sort eg:
Collections.sort(list);
If you need some custom sorting you can implement your own comparator:
Collections.sort(list, new Comparator<String>() {
public int compare(String s1, String s2) {
// some own compare method...
}
});
Upvotes: 1