[Java] Sorting tips when strings and numbers are mixed

Continuing from the previous [Java 8] Proper use of Comparable and Comparator from the viewpoint of employee sorting, these are tips for sorting.

I couldn't find much when I searched for it,

["2", "32", "100", "13", "1", "21"]

Such an array

["A02", "12", "A11", "A01", "B01", "A21"]

I want to sort this array ** "Yoshinani" **! Have you ever thought?

It is troublesome to parse numbers one by one, and when parsed, it is troublesome to control when it is not a number. It's not a big deal, but I thought it could be solved with a relatively simple process, so I'll write down the process.

Expected sort result


["1", "2", "13", "21", "32", "100"]
// ->Before sorting["2", "32", "100", "13", "1", "21"]

["12", "A01", "A02", "A11", "A21", "B01"]
// ->Before sorting["A02", "12", "A11", "A01", "B01", "A21"]

To conclude, ** first compare the lengths of the strings, then compare the strings **.

I will actually write it.

Compare the lengths of the strings, then compare the strings


pubic List<String> sort(List<String> list) {
    list.sort((s1, s2) -> {
        if (compareLength(s1, s2) == 0) {
            return s1.compareTo(s2);
        }
        return compareLength(s1, s2);
    });
    return list;
}

private int compareLength(String s1, String s2) {
    if (s1.length() > s2.length()) {
        return 1;
    } else if (s1.length() < s2.length()) {
        return -1;
    } else {
        return 0;
    }
}

Execution result


List<String> list1 = new ArrayList<>(Arrays.asList("2", "32", "100", "13", "1", "21"));↲
List<String> list2 = new ArrayList<>(Arrays.asList("A02", "12", "A11", "A01", "B01", "A21"));↲
List<String> list3 = new ArrayList<>(Arrays.asList("0002", "0032", "0100", "0013", "0001", "0021"));↲

System.out.println(ArrayUtils.toString(sort(list1)));
System.out.println(ArrayUtils.toString(sort(list2)));

//Execution result
[1, 2, 13, 21, 32, 100]
[12, A01, A02, A11, A21, B01]

By the way, even if the digits are padded with 0s, if the number of digits is the same, it will sort as expected.

When filling with 0


List<String> list = new ArrayList<>(Arrays.asList("0002", "0032", "0100", "0013", "0001", "0021"));↲

System.out.println(ArrayUtils.toString(sort(list)));

//Execution result
[0001, 0002, 0013, 0021, 0032, 0100]

I don't think it can be used in any situation because it says ** "Yoshinani" **, but personally I am satisfied with the results I expected. If you have logic that can be processed smarter, or if you have a list that can not be processed "good" with this method, I would be grateful if you could share it!

Postscript

He commented that it would be simpler if you did the following!

List<String> list = Arrays.asList("2", "32", "100", "13", "1", "21");
list.sort(Comparator.comparing(String::length).thenComparing(Function.identity());
System.out.println(list);

Recommended Posts

[Java] Sorting tips when strings and numbers are mixed
[Java] What are overrides and overloads?
[Java] Tips and error issues when converting from double to Big Decimal
Convert Java org.w3c.dom.Document objects and XML strings
Behavior when Java annotation names are worn
java jshell and var are too recommended!
[Java] Precautions when comparing character strings with character strings
Java tips, tips
Java Tips
Java switch statement and break, Kotlin when expression ...
Java tips --Suspect dependencies when JavaMail raises ClassNotFoundException
When there are environment variables in Java tests
Distinguish between positive and negative numbers in Java