public String[] solution(String[] files) {
Arrays.sort(files, (o1, o2) -> {
String[] part1 = modPart(o1);
String[] part2 = modPart(o2);
int headComparing = part1[0].compareTo(part2[0]);
if (headComparing != 0) {
return headComparing;
}
return Integer.compare(Integer.parseInt(part1[1]), Integer.parseInt(part2[1]));
});
return files;
}
private String[] modPart(String s) {
String[] part = new String[3];
boolean state = false;
int numberStartIdx = -1;
int i = 0;
for (char currentChar : s.toCharArray()) {
if (!state && isNumber(currentChar)) {
part[0] = s.substring(0, i).toLowerCase();
numberStartIdx = i;
state = true;
} else if (state && !isNumber(currentChar)) {
String number = s.substring(numberStartIdx, i);
if (number.length() > 5) {
i -= (number.length() - 5);
}
part[1] = s.substring(numberStartIdx, i);
part[2] = s.substring(i);
break;
}
i++;
}
if (part[1] == null) {
String number = s.substring(numberStartIdx, i);
if (number.length() > 5) {
i -= (number.length() - 5);
}
part[1] = s.substring(numberStartIdx, i);
part[2] = s.substring(i);
}
return part;
}
private boolean isNumber(char c) {
return Character.isDigit(c);
}
출처:https://school.programmers.co.kr/learn/courses/30/lessons/17686