Even smarter sorting for file names, i.e [test1.txt, test03.txt, test2.txt, test11.txt, test20.txt]

This commit is contained in:
Bas Leijdekkers
2011-08-19 15:28:46 +02:00
parent 3557e4b43c
commit b2daf005c7
@@ -15,6 +15,8 @@
*/
package com.intellij.ide.util.treeView;
import com.intellij.openapi.util.text.StringUtil;
import java.util.Comparator;
public class AlphaComparator implements Comparator<NodeDescriptor>{
@@ -34,28 +36,6 @@ public class AlphaComparator implements Comparator<NodeDescriptor>{
if (s1 == null) return s2 == null ? 0 : -1;
if (s2 == null) return +1;
if (startsWithDigit(s1) && startsWithDigit(s2)) {
int p1 = calculatePrefix(s1);
int p2 = calculatePrefix(s2);
if (p1 != p2) return p1 - p2;
}
return s1.compareToIgnoreCase(s2);
}
private static int calculatePrefix(String s) {
int acc = 0;
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if (c < '0' || c > '9') break;
acc = acc * 10 + c - '0';
}
return acc;
}
private static boolean startsWithDigit(String s) {
if (s.isEmpty()) return false;
char c = s.charAt(0);
return c >= '0' && c <= '9';
return StringUtil.naturalCompare(s1, s2);
}
}