How to sort Alphanumeric String

前端 未结 11 1445
我寻月下人不归
我寻月下人不归 2020-12-10 06:25

I have a problem with sorting strings which include integers. If I use the below code I get sorting like: 1some, 2some, 20some, 21some, 3some, some

However I want it

11条回答
  •  死守一世寂寞
    2020-12-10 06:54

    Here is a self-contained example on how to do this (not particularly optimized):

    final Pattern p = Pattern.compile("^\\d+");
    String[] examples = { 
       "1some", "2some", "20some", "21some", "3some", "some", "1abc", "abc"
    };
    Comparator c = new Comparator() {
        @Override
        public int compare(String object1, String object2) {
            Matcher m = p.matcher(object1);
            Integer number1 = null;
            if (!m.find()) {
                return object1.compareTo(object2);
            }
            else {
                Integer number2 = null;
                number1 = Integer.parseInt(m.group());
                m = p.matcher(object2);
                if (!m.find()) {
                    return object1.compareTo(object2);
                }
                else {
                    number2 = Integer.parseInt(m.group());
                    int comparison = number1.compareTo(number2);
                    if (comparison != 0) {
                        return comparison;
                    }
                    else {
                        return object1.compareTo(object2);
                    }
                }
            }
        }
    };
    List examplesList = new ArrayList(Arrays.asList(examples));
    Collections.sort(examplesList, c);
    System.out.println(examplesList);
    

    Output

    [1abc, 1some, 2some, 3some, 20some, 21some, abc, some]
    

    Explanation

    • The example uses a constant Pattern to infer whether a number is in the String's starting position.
    • If not present in the first String, it compares it as is to the second.
    • If present indeed in the first, it checks the second.
    • If not present in the second, it compares the two Strings as is, again
    • If present in both, it compares the Integers instead of the whole Strings, hence resulting in a numerical comparison rather than a lexicographical one
    • If the number compare identical, it goes back to lexicographic comparison of the whole Strings (thanks MihaiC for spotting this one)

提交回复
热议问题