Sort on a string that may contain a number

前端 未结 23 2079
走了就别回头了
走了就别回头了 2020-11-22 02:59

I need to write a Java Comparator class that compares Strings, however with one twist. If the two strings it is comparing are the same at the beginning and end of the strin

23条回答
  •  自闭症患者
    2020-11-22 03:28

    My problem was that I have lists consisting of a combination of alpha numeric strings (eg C22, C3, C5 etc), alpha strings (eg A, H, R etc) and just digits (eg 99, 45 etc) that need sorting in the order A, C3, C5, C22, H, R, 45, 99. I also have duplicates that need removing so I only get a single entry.

    I'm also not just working with Strings, I'm ordering an Object and using a specific field within the Object to get the correct order.

    A solution that seems to work for me is :

    SortedSet codeSet;
    codeSet = new TreeSet(new Comparator() {
    
    private boolean isThereAnyNumber(String a, String b) {
        return isNumber(a) || isNumber(b);
    }
    
    private boolean isNumber(String s) {
        return s.matches("[-+]?\\d*\\.?\\d+");
    }
    
    private String extractChars(String s) {
        String chars = s.replaceAll("\\d", "");
        return chars;
    }
    
    private int extractInt(String s) {
        String num = s.replaceAll("\\D", "");
        return num.isEmpty() ? 0 : Integer.parseInt(num);
    }
    
    private int compareStrings(String o1, String o2) {
    
        if (!extractChars(o1).equals(extractChars(o2))) {
            return o1.compareTo(o2);
        } else
            return extractInt(o1) - extractInt(o2);
    }
    
    @Override
    public int compare(Code a, Code b) {
    
        return isThereAnyNumber(a.getPrimaryCode(), b.getPrimaryCode()) 
                ? isNumber(a.getPrimaryCode()) ? 1 : -1 
                    : compareStrings(a.getPrimaryCode(), b.getPrimaryCode());
                    }
                });
    

    It 'borrows' some code that I found here on Stackoverflow plus some tweaks of my own to get it working just how I needed it too.

    Due to trying to order Objects, needing a comparator as well as duplicate removal, one negative fudge I had to employ was I first have to write my Objects to a TreeMap before writing them to a Treeset. It may impact performance a little but given that the lists will be a max of about 80 Codes, it shouldn't be a problem.

提交回复
热议问题