Skip to content

Commit 7b9fad7

Browse files
authored
Merge pull request #1442 from AlgorithmWithGod/lkhyun
[20251118] PGM / Lv2 / [3차] 파일명 정렬 / 이강현
2 parents f5b8a7a + f7cbd2a commit 7b9fad7

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
```java
2+
import java.util.stream.*;
3+
class Solution {
4+
static class File implements Comparable<File>{
5+
String fileName;
6+
String HEAD;
7+
int NUMBER;
8+
int index;
9+
10+
File(String fileName, int index){
11+
this.fileName = fileName;
12+
13+
int i = 0;
14+
while(i<fileName.length() && !Character.isDigit(fileName.charAt(i))) i++;
15+
HEAD = fileName.substring(0,i);
16+
17+
int j = i;
18+
while(j<fileName.length() && Character.isDigit(fileName.charAt(j))) j++;
19+
NUMBER = Integer.parseInt(fileName.substring(i,j));
20+
21+
this.index = index;
22+
}
23+
24+
@Override
25+
public int compareTo(File o){
26+
int compare1 = this.HEAD.toLowerCase().compareTo(o.HEAD.toLowerCase());
27+
if(compare1 != 0) return compare1;
28+
29+
int compare2 = Integer.compare(this.NUMBER, o.NUMBER);
30+
if(compare2 != 0) return compare2;
31+
32+
return Integer.compare(this.index, o.index);
33+
}
34+
35+
}
36+
public String[] solution(String[] files) {
37+
return IntStream.range(0,files.length)
38+
.mapToObj(i -> new File(files[i],i))
39+
.sorted()
40+
.map(i -> i.fileName)
41+
.toArray(String[]::new);
42+
}
43+
}
44+
```

0 commit comments

Comments
 (0)