gpt4 book ai didi

java - Collat​​or Locale German - 如何仅在正常字母之后对重音字母进行排序

转载 作者:行者123 更新时间:2023-12-05 07:43:10 25 4
gpt4 key购买 nike

我已经使用 Collat​​or 对对象数组进行排序。但我发现它像对待普通字母一样对待重音字母:

Aktivierung
Änderung
Auszahlung
Bar

相反,我想要这个

Aktivierung
Auszahlung
Änderung
Bar

重音字母应该紧跟在普通字母之后。这也适用于 o/ö 和 u/ü。

Collator collator = Collator.getInstance(Locale.GERMAN);
...
private void sortDocumentiByCategoria(final Collator collator, List<ListDocumenti> listDocumenti) {
Collections.sort(listDocumenti, new Comparator<ListDocumenti>(){
@Override
public int compare(ListDocumenti arg0, ListDocumenti arg1) {
return collator.compare(arg0.getDescrizione(), arg1.getDescrizione());
}
});
}

最佳答案

花了一些时间,但我想通了。给你!

public static void main(String[] args) throws IOException {
List<String> list = Arrays.asList("Änderung", "Aktivierung", "Auszahlung", "Bar");

Collections.sort(list, createCollator());

System.out.println(list);
}

private static RuleBasedCollator createCollator() {
String german = "" +
"= '-',''' " +
"< 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" +
"& ss=ß";

try {
return new RuleBasedCollator(german);
} catch (ParseException e) {
throw new RuntimeException(e);
}
}

测试它会产生以下结果:

>> [Aktivierung, Auszahlung, Änderung, Bar]

关于java - Collat​​or Locale German - 如何仅在正常字母之后对重音字母进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43931170/

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