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 characters.
Comparator<SetGetMethods> comperator = new Comparator<SetGetMethods>() {
public int compare(SetGetMethods object1, SetGetMethods object2) {
return object1.getCityname().compareToIgnoreCase(object2.getCityname());
}
};
Collections.sort(temp, comperator);
I used the code above to sort the list alphabetically. But after sorting , the names with normal characters shown on top and names with special norwegian characters shown below them. For example , the list is shown like below ,
Arendal Bergen Drammen Ålesund -> ( This should be on top , before Arendal after sorting )
So , my question is , how can i sort a list alphabetically where the list data contains special characters(Norwegian characters). I will appreciate any suggestion , idea or sample code to solve the problem. Thanks .....
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:
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.Try this
For an Object
If you want to order by name (in this case using Norwegian language), you can do this