forked from GreatAlgorithm-Study/AlgorithmStudy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDH_17686.java
More file actions
70 lines (55 loc) · 1.72 KB
/
DH_17686.java
File metadata and controls
70 lines (55 loc) · 1.72 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import java.util.*;
/*
* 파일명 정렬
*/
public class DH_17686 {
static class FileInfo implements Comparable<FileInfo> {
int idx;
String head;
int number;
public FileInfo(int idx, String head, String number) {
this.idx = idx;
this.head = head.toLowerCase();
this.number = Integer.parseInt(number);
}
@Override
public int compareTo(FileInfo o) {
if(this.head.equals(o.head)) return Integer.compare(this.number, o.number);
return this.head.compareTo(o.head);
}
}
static ArrayList<FileInfo> fileList;
static String[] solution(String[] files) {
String[] answer = new String[files.length];
fileList = new ArrayList<>();
// 파일명 구성: HEAD, NUMBER, TAIL
for(int idx = 0; idx < files.length; idx++) {
String file = files[idx];
// HEAD 구성하기
int currentIdx = 0;
for(int i = 0; i < file.length(); i++) {
char ch = file.charAt(i);
if(ch - '0' >= 0 && ch - '0' < 10) break;
currentIdx += 1;
}
String head = file.substring(0, currentIdx);
// NUMBER 구성하기
int startIdx = currentIdx;
for(int i = currentIdx; i < file.length(); i++) {
char ch = file.charAt(i);
if(ch - '0' < 0 || ch - '0' >= 10) break;
currentIdx += 1;
}
String number = file.substring(startIdx, currentIdx);
fileList.add(new FileInfo(idx, head, number));
// System.out.println(head + " " + number);
}
Collections.sort(fileList);
for(int i = 0; i < fileList.size(); i++) answer[i] = files[fileList.get(i).idx];
return answer;
}
public static void main(String[] args) {
String[] files = {"img12.png", "img10.png", "img02.png", "img1.png", "IMG01.GIF", "img2.JPG"};
System.out.println(Arrays.toString(solution(files)));
}
}