-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcompareSort.java
More file actions
44 lines (37 loc) · 1.33 KB
/
compareSort.java
File metadata and controls
44 lines (37 loc) · 1.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
public static class NumberAwareStringComparator implements Comparator<CharSequence> {
public static final NumberAwareStringComparator INSTANCE =
new NumberAwareStringComparator();
private static final Pattern PATTERN = Pattern.compile("(\\D*)(\\d*)");
private NumberAwareStringComparator() {
}
public int compare(CharSequence s1, CharSequence s2) {
Matcher m1 = PATTERN.matcher(s1);
Matcher m2 = PATTERN.matcher(s2);
// The only way find() could fail is at the end of a string
while (m1.find() && m2.find()) {
// matcher.group(1) fetches any non-digits captured by the
// first parentheses in PATTERN.
int nonDigitCompare = m1.group(1).compareTo(m2.group(1));
if (0 != nonDigitCompare) {
return nonDigitCompare;
}
// matcher.group(2) fetches any digits captured by the
// second parentheses in PATTERN.
if (m1.group(2).isEmpty()) {
return m2.group(2).isEmpty() ? 0 : -1;
} else if (m2.group(2).isEmpty()) {
return +1;
}
BigInteger n1 = new BigInteger(m1.group(2));
BigInteger n2 = new BigInteger(m2.group(2));
int numberCompare = n1.compareTo(n2);
if (0 != numberCompare) {
return numberCompare;
}
}
// Handle if one string is a prefix of the other.
// Nothing comes before something.
return m1.hitEnd() && m2.hitEnd() ? 0 :
m1.hitEnd() ? -1 : +1;
}
}