I have a problem with sorting strings which include integers. If I use the below code I get sorting like: 1some, 2some, 20some, 21some, 3some, some
However I want it
Here is a self-contained example on how to do this (not particularly optimized):
final Pattern p = Pattern.compile("^\\d+");
String[] examples = {
"1some", "2some", "20some", "21some", "3some", "some", "1abc", "abc"
};
Comparator c = new Comparator() {
@Override
public int compare(String object1, String object2) {
Matcher m = p.matcher(object1);
Integer number1 = null;
if (!m.find()) {
return object1.compareTo(object2);
}
else {
Integer number2 = null;
number1 = Integer.parseInt(m.group());
m = p.matcher(object2);
if (!m.find()) {
return object1.compareTo(object2);
}
else {
number2 = Integer.parseInt(m.group());
int comparison = number1.compareTo(number2);
if (comparison != 0) {
return comparison;
}
else {
return object1.compareTo(object2);
}
}
}
}
};
List examplesList = new ArrayList(Arrays.asList(examples));
Collections.sort(examplesList, c);
System.out.println(examplesList);
Output
[1abc, 1some, 2some, 3some, 20some, 21some, abc, some]
Explanation
Pattern to infer whether a number is in the String's starting position.String, it compares it as is to the second.Strings as is, againIntegers instead of the whole Strings, hence resulting in a numerical comparison rather than a lexicographical oneStrings (thanks MihaiC for spotting this one)