Reputation: 71
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).
Upvotes: 0
Views: 572
Reputation:
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?
Upvotes: 1
Reputation: 140494
Parse the number out:
Comparator.comparingInt(v -> Integer.parseInt(v.substring(1))
(assuming the prefix is always 1 character)
Upvotes: 0