简体   繁体   English

使用特殊字符(例如重音符号)对数组进行排序

[英]Ordering an array with special characters like accents

Does somebody knows how to order an array with words with special characters like accents?有人知道如何使用带有特殊字符(如重音)的单词对数组进行排序吗?

Arrays.sort(anArray);

returns 'Albacete' before 'Álava' , and I want 'Álava' before 'Albacete' .回报'Albacete''Álava' ,我想'Álava''Albacete'

Thanks a lot非常感谢

If you just want to sort the strings as if they didn't have the accents, you could use the following:如果您只想对字符串进行排序,就好像它们没有重音一样,您可以使用以下命令:

Collections.sort(strs, new Comparator<String>() {
    @Override
    public int compare(String o1, String o2) {
        o1 = Normalizer.normalize(o1, Normalizer.Form.NFD);
        o2 = Normalizer.normalize(o2, Normalizer.Form.NFD);
        return o1.compareTo(o2);
    }
});

Related question:相关问题:

For more sophisticated use cases you will want to read up on java.text.Collator .对于更复杂的用例,您需要阅读java.text.Collator Here's an example:下面是一个例子:

Collections.sort(strs, new Comparator<String>() {
    @Override
    public int compare(String o1, String o2) {
        Collator usCollator = Collator.getInstance(Locale.US);
        return usCollator.compare(o1, o2);
    }
});

If none of the predefined collation rules meet your needs, you can try using the java.text.RuleBasedCollator .如果没有预定义的整理规则满足您的需求,您可以尝试使用java.text.RuleBasedCollator

You should take a look at RuleBasedCollator你应该看看RuleBasedCollat​​or

RuleBasedCollator class is a concrete subclass of Collator that provides a simple, data-driven, table collator. RuleBasedCollat​​or 类是 Collat​​or 的具体子类,它提供了一个简单的、数据驱动的表格整理器。 With this class you can create a customized table-based Collator.使用这个类,您可以创建一个自定义的基于表格的 Collat​​or。 RuleBasedCollator maps characters to sort keys. RuleBasedCollat​​or 将字符映射到排序键。

RuleBasedCollator has the following restrictions for efficiency (other subclasses may be used for more complex languages) : RuleBasedCollat​​or 对效率有以下限制(其他子类可能用于更复杂的语言):

If a special collation rule controlled by a is specified it applies to the whole collator object.如果指定了由 a 控制的特殊整理规则,则它适用于整个整理器对象。 All non-mentioned characters are at the end of the collation order.所有未提及的字符都位于整理顺序的末尾。

use a comparator like the below :) and sort your list使用如下所示的比较器 :) 并对您的列表进行排序

Comparator<String> accentIgnorantComparator = (o1, o2) -> {
    return StringUtils.stripAccents(o1).compareTo(StringUtils.stripAccents(o2));
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在字节数组中检测诸如^ A之类的特殊字符 - How to detect special characters like ^A in a byte array 向数组添加特殊字符 - Adding special characters to an array 在 Java 中转义像 + 这样的特殊字符 - Escape special characters like + in Java 使用正则表达式剥离字符失败,而使用带有变音符号,撇号,重音符号等的文字字符 - Stripping characters using a regex fails using literal characters with diacritics, apostrophes, accents, and the like 如何在URL的jsp中传递特殊字符,例如&+% - how to pass special characters like & + % in jsp in URL iText不喜欢我的特殊字符 - iText doesn't like my special characters 在java中将%和$等特殊字符转换为double - convert special characters like % and $ to double in java indexOf()与regex一起用于识别特殊字符,例如$和{ - indexOf() vs regex for identifying special characters like $ and { 如何从 URL 字符串数组中删除包含特殊字符(如“#”)和文本块(如“.pdf”)的 URL? - How can I get rid of urls that contain special characters like, "#", and text chunks like, ".pdf" from a string array of urls? 在GWT中用特殊字符(德语Umlaute)对数组进行排序 - sorting an array with special characters (german Umlaute) in GWT
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM