如何用Java中的挪威字符排序字符串?

时间:2016-09-01 09:44:42

标签: java sorting localization locale setlocale

æøå是挪威字母表中的最新字母

   A B C D E F G H I J K L M N O P Q R S T U V W X Y Z Æ Ø Å

    List<String> words = Arrays.asList(
    "A", "B", "Z", "Æ", "Ø", "Å"     );

    Locale la = new Locale("nor", "NOR");
    Collator coll = Collator.getInstance(la);
    coll.setStrength(Collator.PRIMARY);
    Collections.sort(words, coll);
    System.out.println(""+ words);

答案应该是

A,B,Z,Æ,ØÅ,

但我得到了:

A,Å,Æ,B,Z,Ø

有人可以建议如何获得以上输出吗?

1 个答案:

答案 0 :(得分:5)

语言环境错误。对于挪威语,语言为“不”,国家为“否”

    List<String> words = Arrays.asList(
        "Abba", "B", "BØ", "BÆ", "Z", "Æ", "Ø", "Å"     );

    Locale la = new Locale("no", "NO");
    Collator coll = Collator.getInstance(la);
    coll.setStrength(Collator.PRIMARY);
    Collections.sort(words, coll);
    System.out.println(""+ words);

正确输出:[Abba,B,BÆ,BØ,Z,Æ,Ø,Å]