按字典顺序这是正确的排序,因为 character 9 在 character 1 之后。您应该将这些字符串拆分为非数字和数字部分,并分别对它们进行排序:数字作为数字,字符串作为字符串。排序后,您可以将这些部分重新组合成一个字符串并获得一个排序数组。例如:
// 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]
另见:
• How do I relate one array input to another?
•How to remove sequence of two elements from array or list?