简体   繁体   中英

How do I sort lexicographically with sorted(comparator) method?

I have this function that sorts my lines of data by numerical order, but the problem is that the key it sorts by composed from letters and numbers:

void writeData(Map<String, Params> data) {
    .
    .
    StringBuilder sb = new StringBuilder();
    data.values()
        .stream()
        .sorted(Comparator.comparing(Params::getIdx))
        .forEach(r -> {
            sb.append(System.lineSeparator());
            sb.append(r.getCSVRow());
        });
    .
    .
}

For example I have the strings: "A100","A101", "A99" .

The order I get:

A100
A101
A99

The order I want:

A99
A100
A101

How can I change this function? I don't have an option to use thenCompare for some reason. (I've read about it though).

Lexicographically this is the correct sorting, because character 9 goes after character 1 . You should split these strings into non-numeric and numeric parts and sort them separately: numbers as numbers, and strings as strings. After sorting you can join these parts back into one string and get a sorted array. For example:

// assume a string consists of a sequence of
// non-numeric characters followed by numeric characters
String[] arr1 = {"A100", "A101", "A99", "BBB33", "C10", "T1000"};
String[] arr2 = Arrays.stream(arr1)
        // split string into an array of two
        // substrings: non-numeric and numeric
        // Stream<String[]>
        .map(str -> str
                // add some delimiters to a non-empty
                // sequences of non-numeric characters
                .replaceAll("\\D+", "$0\u2980")
                // split string into an array
                // by these delimiters
                .split("\u2980"))
        // parse integers from numeric substrings,
        // map as pairs String-Integer
        // Stream<Map.Entry<String,Integer>>
        .map(row -> Map.entry(row[0], Integer.parseInt(row[1])))
        // sort by numbers as numbers
        .sorted(Map.Entry.comparingByValue())
        // intermediate output
        //C=10
        //BBB=33
        //A=99
        //A=100
        //A=101
        //T=1000
        .peek(System.out::println)
        // join back into one string
        .map(entry -> entry.getKey() + entry.getValue())
        // get sorted array
        .toArray(String[]::new);
// final output
System.out.println(Arrays.toString(arr2));
// [C10, BBB33, A99, A100, A101, T1000]

See also:
How do I relate one array input to another?
How to remove sequence of two elements from array or list?

Parse the number out:

Comparator.comparingInt(v -> Integer.parseInt(v.substring(1))

(assuming the prefix is always 1 character)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM