1

I am developing an android application for market survey. On the registration page I have a spinner to list down all the countries and I am using the function Arrays.sort() to sort the list in alphabetically ascending order. I kept my tablet language as 'English' and obtained an alphabetically sorted list.

However, when I changed the tablet language to Portuguese, the list being displayed is not sorted (please see the screenshot attached). The language has some alphabets different from the English language. I know that java performs sorting based on ASCII code of the characters. But is there any other way to obtain a sorted result, irrespective of the language being used.

enter image description here

Kevin Cruijssen
  • 9,153
  • 9
  • 61
  • 135
Abhishek Madan
  • 165
  • 2
  • 10
  • 2
    It looks like your list is indeed sorted based on the language. It's only that the character code of Á is greater than that of Z. You might want to consider this solution: http://stackoverflow.com/a/12889868/578759 – Hok Apr 29 '16 at 09:37

2 Answers2

3

I think you are looking for a Collator with a specific chosen Locale (like English, since you've stated that it then works correctly).

So you can use something like this:

Collections.sort(yourArray, Collator.getInstance(new Locale("en", "US")));
Kevin Cruijssen
  • 9,153
  • 9
  • 61
  • 135
0

I think you need to expect some seemingly arbitrariness when sorting letters in a locale who's language does not include those letters. A collation might specify an ordering but not follow rules that you would like it to. For example, in US English, what is the correct ordering between ñ and ณ?

One option is the have a separate list in each language that you want to sort by, using only letters in that language. (If that doesn't work, then, once you've gone that far, you could have a fixed ordering in each list.)

Tom Blodget
  • 20,260
  • 3
  • 39
  • 72