File tree Expand file tree Collapse file tree 1 file changed +44
-0
lines changed
Expand file tree Collapse file tree 1 file changed +44
-0
lines changed Original file line number Diff line number Diff line change 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+ ```
You can’t perform that action at this time.
0 commit comments