Created
November 16, 2019 09:52
-
-
Save criskgl/41657aa72945c9f5437407337360abb8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public String[] reorderLogFiles(String[] logs) { | |
if (logs == null || logs.length == 0) return logs; | |
int len = logs.length; | |
List<String> letterList = new ArrayList<>(); | |
List<String> digitList = new ArrayList<>(); | |
for (String log : logs) { | |
if (log.split(" ")[1].charAt(0) < 'a') { | |
digitList.add(log); | |
} else { | |
letterList.add(log); | |
} | |
} | |
Collections.sort(letterList, (o1, o2) -> { | |
String[] s1 = o1.split(" "); | |
String[] s2 = o2.split(" "); | |
int len1 = s1.length; | |
int len2 = s2.length; | |
for (int i = 1; i < Math.min(len1, len2); i++) { | |
if (!s1[i].equals(s2[i])) { | |
return s1[i].compareTo(s2[i]); | |
} | |
} | |
//tie --> use identifier | |
return s1[0].compareTo(s2[0]); | |
}); | |
for (int i = 0; i < len; i++) { | |
if (i < letterList.size()) | |
logs[i] = letterList.get(i); | |
else logs[i] = digitList.get(i - letterList.size()); | |
} | |
return logs; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment