首页 文章

通过忽略beginnig中的 - 字符(如果存在)对arraylist进行排序

提问于
浏览
-1

我想对包含许多行的arraylist进行排序

我想忽略 ' - '

if it is the first letter of that line

INPUT

-bbb

A-AAA

滴滴滴

B-DDD

C-CCC

output

should be (as u can see it should be sorted on first character(a b c d )

A-AAA

-bbb

C-CCC

滴滴滴

E-EEE

here is the code i have

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

});

4 回答

  • 1
    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));
    
  • 0

    你用|在比较器而不是 - .

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

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

    这将忽略许多结果....


    To compare correctly you must follow this behaviour :如果 String- 开头,则获得第二个字符(位置1),否则取第一个字符(位置0) . 只要你不能 compareTo 原始类型, substring 的单个字符会更有用 .

    分别为每个字符串执行此操作,并为结果执行 compareTo .


    知道了这一点,你可以实现你的比较器(还有其他的减少和oneliners答案,但为了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);
        }
    });
    

    Main for test:

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

    Output:

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

    老实说,没有必要做那么多工作 . 只需在使用正则表达式时比较两个值 . 你最初遇到的问题是,当他们以 - 开头时,你从来没有忽略过它们,如果它确实以 - 开头,你只是比另一个更大了一个lexograhpply

    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
    
  • 0

    这是您的要求的逻辑

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

相关问题