【问题标题】:Sort arraylist by ignoring the - character in the beginnig if present通过忽略当前开头的 - 字符对数组列表进行排序
【发布时间】:2023-03-15 01:42:01
【问题描述】:

我想对包含多行的数组列表进行排序

我想忽略 ' - '

如果它是该行的第一个字母

输入

-bbb

啊啊啊

-ddd

b-ddd

c-ccc

输出

应该是(如您所见,它应该按第一个字符排序(a b c d)

啊啊啊

-bbb

c-ccc

-ddd

e-eee

这是我的代码

Collections.sort(arrList, new Comparator<String>() {

@Override
public int compare(String o1, String o2) {
    if (o1.startsWith("-") && o2.startsWith("-")) {
        return compare(o1.substring(1), o2.substring(1));
    }

    if (o1.startsWith("-")) {
        return 1;
    }
    if (o2.startsWith("-")) {
        return -1;
    }

    return o1.compareTo(o2);
}

});

【问题讨论】:

  • 在您的描述中您使用-,但您的代码使用| - 这应该如何工作?
  • 如果它以| 开头,你也可以立即使一个字典比另一个大,但以后不要关心字典值。您的问题询问如何忽略它们,您的代码告诉我们不同。

标签: java sorting arraylist


【解决方案1】:

你在比较器中使用|,而不是-

您的比较不正确,如果o1- 开头,则不能忽略o2

if (o1.startsWith("-")) {
    return 1;
}

这会忽略很多结果....


要正确比较,您必须遵循以下行为:如果 String- 开头,则获取第二个字符(位置 1),否则获取第一个字符(位置 0)。只要你不能compareTo原始类型,单个字符的substring会更有用。

分别对每个字符串执行此操作并compareTo 结果。


知道了这一点,您可以像这样实现比较器(还有其他简化和单行的答案,但为了让 OP 可以更好地理解我将是基本的):

Collections.sort(arrList, new Comparator<String>() {

    @Override
    public int compare(String o1, String o2) {
        String s1 = "", s2 = "";

        // check first string and choose 
        //candidate character to compare
        if (o1.startsWith("-")) 
            s1 = o1.substring(1);
        else 
            s1 = o1.substring(0);

        // check second string and choose 
        // candidate character to compare
        if (o2.startsWith("-")) 
            s2 = o2.substring(1);
        else 
            s2 = o2.substring(0);

        // compare both choosen characters        
        return s1.compareTo(s2);
    }
});

主要测试:

List<String> list = new ArrayList<>();
list.add("-bbb");
list.add("a-aaa");
list.add("-ddd");
list.add("b-ddd");
list.add("c-ccc");

输出:

a-aaa
b-ddd
-bbb
c-ccc
-ddd

【讨论】:

    【解决方案2】:

    老实说,没有必要做这么多工作。只需在使用正则表达式时比较两个值。您最初遇到的问题是,当它们确实以 - 开头时,您永远不会忽略它们,如果它确实以 - 开头,您只是使一个词典比另一个大。

    public static void main(String[] args) {
        List<String> testList = new ArrayList<>();
        testList.add("-bbb");
        testList.add("a-aaa");
        testList.add("-ddd");
        testList.add("b-ddd");
        testList.add("c-ccc");
        Collections.sort(testList, new Comparator<String>() {
    
            @Override
            public int compare(String o1, String o2) {
                // This removes leading - from the input and ignores them
                // At the moment it removes one ore more -, if it should just be one
                // remove the +
                return o1.replaceAll("^-+", "").compareTo(o2.replaceAll("^-+", ""));
            }
        });
        for(String s : testList) {
            System.out.println(s);
        }
    }
    

    O/P

    a-aaa
    b-ddd
    -bbb
    c-ccc
    -ddd
    

    【讨论】:

    • @JordiCastilla 哦,是的,只是在其中写了随机值,并将它们交换为问题中 OP 使用的值,但没有更改 .add 输入。谢谢提示
    【解决方案3】:
    final String o1s;
    final String o2s;
    
    if (o1.startsWith("|")) {
        o1s = o1.substring(1);
    } else {
        o1s = o1;
    }
    
    if (o2.startsWith("|")) {
        o2s = o2.substring(1);
    } else {
        o2s = o2;
    }
    
    return o1s.compareTo(o2s);
    

    或者,更短但可读性较差

    int o1offset = o1.startsWith("|") ? 1 : 0;
    int o2offset = o2.startsWith("|") ? 1 : 0;
    
    return o1.substring(o1offset).compareTo(o2.substring(o2offset));
    

    【讨论】:

    • 很高兴为一个不正确的答案感到自豪,只是因为被接受了......通过- 更正|符号以帮助未来的用户会比发布这个愚蠢的帖子更有用(和更快)评论....顺便说一句:downvote 是我的,仍在等待您的编辑被删除...
    【解决方案4】:

    这是满足您要求的逻辑

    public int compare(String str1, String str2) {
            if(str1.startsWith("-")){
                if(str2.startsWith("-")){
                    str1 = str1.substring(1);
                    str2 = str2.substring(1);
    
                    return str1.compareTo(str2);
                }else{
                    str1 = str1.substring(1);
                    return str1.compareTo(str2);
                }
            } else if(str2.startsWith("-")){
                if(str1.startsWith("-")){
                    str1 = str1.substring(1);
                    str2 = str2.substring(1);
    
                    return str1.compareTo(str2);
                }else{
                    str2 = str2.substring(1);
                    return str1.compareTo(str2);
                }
            }else{
                return str1.compareTo(str2);
            }
        }
    

    【讨论】:

    • 这太复杂了。
    猜你喜欢
    • 2018-05-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-07
    • 1970-01-01
    • 2016-01-08
    • 2017-09-26
    相关资源
    最近更新 更多