Android : How To Sort An Arraylist Of Special Characters Alphabetically?
I am developing an android application where i have data in Norwegian language. Now , i have to sort a list of name alphabetically where the names contain special norwegian charact
Solution 1:
Try this
For an Object
classMyObject{
publicString name;
publicint score;
}
If you want to order by name (in this case using Norwegian language), you can do this
Localelocale=newLocale("nb", "NO");
finalCollatorcollator= java.text.Collator.getInstance(locale);
Collections.sort(your_array, newComparator<MyObject>() {
@Overridepublicintcompare(MyObject obj1, MyObject obj2) {
return collator.compare(obj1.name, obj2.name);
}
});
Solution 2:
Take a look at the example based on Norwegian in the JavaDoc for the RuleBasedCollator class.
Based on that, I've created this example that puts Å before A based on a accent difference -- note the use of ';' to put \u00E5 before A. So this works for your example input, but you'll need to add other accented Norwegian characters based on your knowledge of the language to complete the norwegian
comparison string.
String norwegian = "< a, \u00E5;A< b,B< c,C< d,D< e,E< f,F< g,G< h,H< i,I< j,J" +
"< k,K< l,L< m,M< n,N< o,O< p,P< q,Q< r,R< s,S< t,T" +
"< u,U< v,V< w,W< x,X< y,Y< z,Z";
RuleBasedCollator myNorwegian = new RuleBasedCollator(norwegian);
List<String> words =
Arrays.asList("Arendal Bergen Drammen \u00E5lesund".split("\\s"));
System.out.println(words);
Collections.sort(words, myNorwegian);
System.out.println(words);
Solution 3:
Use a Collator. This is a type of Comparator that performs locale-sensitive String comparison.
Without knowing about the Norwegian language/characters, I think you'd want to use the following code:
CollatornoCollator= Collator.getInstance(newLocale("no", "NO"));
noCollator.setStrength(Collator.SECONDARY);
...
Post a Comment for "Android : How To Sort An Arraylist Of Special Characters Alphabetically?"