使用Java进行语言排序(德语) [英] Linguistic sorting (German) with Java

查看:148
本文介绍了使用Java进行语言排序(德语)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用数字对字符串进行排序的方式与一种语言不同。例如,英文数字在升序排序中位于字母之前。但是,在德语中,数字在字母之后排序。

Sorting a string with number is done differently from one language to another. For example, in English digits come before letters in an ascending sorting. But, in German, digits are ascendant sorted after letters.

我尝试使用 Collat​​or 如下:

I tried to sort strings using a Collator as follows:

private Collator collator = Collator.getInstance(Locale.GERMANY);
collator.compare(str1, str2)

但以上比较未考虑数字在字母规则之后。

But above comparison does not take into account digits after letters rule.

是否有人知道为什么Java在我使用时不考虑此规则(字母后面的数字) RuleBasedCollat​​or 如下:

Does any one have an idea why Java is not taking this rule (digits after letter) into account for the time being I am using RuleBasedCollator as follows:

private final String sortOrder = "< a, 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 < 0 < 1 < 2 < 3 < 4 < 5 < 6 < 7 < 8 < 9";

private Collator collator = new RuleBasedCollator(sortOrder);


推荐答案

您可以检查/调试源代码以查看为什么没有任何变化:

You can check/debug the source code to see why nothing changes:

Collator.getInstance(Locale.GERMANY);

调用以下代码:

public static synchronized
Collator getInstance(Locale desiredLocale)
{
    // Snipping some code here
    String colString = "";
    try {
        ResourceBundle resource = LocaleData.getCollationData(desiredLocale);

        colString = resource.getString("Rule");
    } catch (MissingResourceException e) {
        // Use default values
    }
    try
    {
        result = new RuleBasedCollator( CollationRules.DEFAULTRULES +
                                        colString,
                                        CANONICAL_DECOMPOSITION );
    }
// Snipping some more code here

在这里你可以看到默认值<(> code> Collat​​ionRules.DEFAULTRULES <)后, / code>)。

Over here you can see that the specific rules (colString which is empty in your case anyway) are placed after the defaults (CollationRules.DEFAULTRULES).

并且您已经发现默认值首先放置数字:

And as you have discovered that defaults have the numerics placed first:

  // NUMERICS

    + "<0<1<2<3<4<5<6<7<8<9"
    + "<\u00bc<\u00bd<\u00be"   // 1/4,1/2,3/4 fractions

    // NON-IGNORABLES
    + "<a,A"
    + "<b,B"
    + "<c,C"
    + "<d,D"

这篇关于使用Java进行语言排序(德语)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆