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?