gpt4 book ai didi

java - 使用 Java 进行语言排序(德语)

转载 作者:太空狗 更新时间:2023-10-29 22:35:17 27 4
gpt4 key购买 nike

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

我尝试使用 Collator 对字符串进行排序如下:

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

但是上面的比较没有考虑字母规则之后的数字。

有没有人知道为什么 Java 在我使用 RuleBasedCollator 时没有考虑这个规则(字母后的数字)?如下:

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);

最佳答案

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

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

在这里您可以看到特定规则(colString 在您的情况下无论如何都是空的)放置在默认值(Collat​​ionRules.DEFAULTRULES之后).

正如您所发现的那样,默认值将数字放在首位:

  // 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 - 使用 Java 进行语言排序(德语),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12778841/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com