head, number, 순을 기준으로 정렬해야 한다.
Arrays.sort(files, (o1, o2) -> {
String head1 = o1.split("[0-9]")[0].toLowerCase();
String head2 = o2.split("[0-9]")[0].toLowerCase();
if (head1.compareTo(head2) == 0) {
int number1 = getNumber(o1.substring(head1.length()));
int number2 = getNumber(o2.substring(head2.length()));
return number1 - number2;
} else {
return head1.compareTo(head2);
}
});
split("[0-9]")
를 사용해서 숫자가 나오기전까지 값을 재 정의 해준다. private static int getNumber(String s) {
StringBuilder result = new StringBuilder();
for (char c : s.toCharArray()) {
if (Character.isDigit(c)) {
result.append(c);
} else {
return Integer.parseInt(result.toString());
}
}
return Integer.parseInt(result.toString());
}
isdigit 을 사용하여 숫자일 경우에만 append를 하고 숫자가 아닌 경우 바로 append된 값을 리턴 해주고 종료된다