在java中对String数组进行排序(特殊方式)

时间:2015-10-19 22:59:17

标签: java arrays sorting uppercase lowercase

我这样做有问题,因为我需要以这种方式对其进行排序:

在:

{aAbB, abBA, AaBb}

后:

{AaBb, aAbB, abBA}

这个想法是在它的小写版本之前排序一个大写字母,并在每个字母之前排序。 我实际上正在使用西班牙语的Collat​​or('ñ'的问题)并将它的强度设置为PRIMARY所以我可以比较两个单词是否相等而不是大写字母。

2 个答案:

答案 0 :(得分:0)

只是做:

private static Comparator<String> ALPHABETICAL_ORDER = new Comparator<String>() {
    public int compare(String str1, String str2) {
        int res = String.CASE_INSENSITIVE_ORDER.compare(str1, str2);
        if (res == 0) {
            res = (str1.compareTo(str2)==0) ? 1 : 0;
        }
        return res;
    }
};

Collections.sort(list, ALPHABETICAL_ORDER);

灵感来自:Simple way to sort strings in the (case sensitive) alphabetical order

答案 1 :(得分:0)

这会产生所需的结果。

Arrays.sort(stringArray) 
相关问题