-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLt937.java
More file actions
61 lines (52 loc) · 1.88 KB
/
Lt937.java
File metadata and controls
61 lines (52 loc) · 1.88 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
package string;
import java.util.*;
class Lt937 {
public String[] reorderLogFiles(String[] logs) {
// 문자 숫자 로그 리스트
List<String> letterLogs = new ArrayList<>();
List<String> digitLogs = new ArrayList<>();
for (String log : logs) {
// 식별자 분리 로직
// indexOf()로 첫번째 공백 인덱스 반환
int space = log.indexOf(" ");
String id = log.substring(0, space);
String content = log.substring(space + 1);
// 문자 숫자 로그 분리 로직
if (Character.isDigit(content.charAt(0))) {
digitLogs.add(log);
} else {
letterLogs.add(log);
}
}
// 문자 로그 정렬
Collections.sort(letterLogs, new Comparator<String>() {
@Override
public int compare(String o1, String o2) {
int space1 = o1.indexOf(" ");
int space2 = o2.indexOf(" ");
String id1 = o1.substring(0, space1);
String content1 = o1.substring(space1 + 1);
String id2 = o2.substring(0, space2);
String content2 = o2.substring(space2 + 1);
// compareTo로 비교.
// -> int의 경우 크다(1), 같다(0), 작다(-1)
// -> 문자열의 경우 아스키값 기준으로 계산.
int comp = content1.compareTo(content2);
if (comp == 0) {
return id1.compareTo(id2);
}
return comp;
}
});
// 배열에 저장
String[] ans = new String[logs.length];
int i = 0;
for (String log : letterLogs) {
ans[i++] = log;
}
for (String log : digitLogs) {
ans[i++] = log;
}
return ans;
}
}