Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Collator doesn't sort right for given Locale

Here's the locale alphabet order: wikipedia
Here's my code:

public static void main(String[] args) {
    Locale loc = new Locale("sr","RS");

    Collator col = Collator.getInstance(loc);
    col.setStrength(Collator.SECONDARY);

    List<String> slova = new ArrayList<String>();

    slova.add("Austrija");
    slova.add("Slovačka");
    slova.add("Č");
    slova.add("Đ");
    slova.add("C");
    slova.add("Grčka");
    slova.add("Slovenija");
    slova.add("Španija");
    slova.add("Švajcarska");
    slova.add("Švedska");
    slova.add("Srbija");

    Collections.sort(slova,col);

    for(String s: slova)
        System.out.println(s);
}

And here's the output:

Austrija
C
Č
Grčka
Slovačka
Slovenija
Španija
Srbija
Švajcarska
Švedska
Đ

As you can see from the link above this is not the correct ordering.
What am I doing wrong?

like image 757
mkvcvc Avatar asked Nov 19 '10 13:11

mkvcvc


2 Answers

As I found on your wikipedia page and @Vash his ISO link. I think you mean by "sr" Serbia? Then you will have to choose "cs" as country.

Edit: it depends on the java version you use. Java 6 uses the new iso standard.

like image 151
Dave Avatar answered Nov 12 '22 08:11

Dave


I think that the problem could be that there is no country in ISO-3166 with code RS

like image 1
Damian Leszczyński - Vash Avatar answered Nov 12 '22 07:11

Damian Leszczyński - Vash