Last active
May 1, 2020 22:12
-
-
Save zeitan/c3208d723c36852364ee81a60a4c4286 to your computer and use it in GitHub Desktop.
sort logs
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
class Pair<T,S> { | |
T fst; | |
S snd; | |
public Pair(T fst, S snd) { | |
this.fst = fst; | |
this.snd = snd; | |
} | |
} |
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
class PairComparator implements Comparator<Pair<String, String>> { | |
@Override | |
public int compare(Pair<String, String> x, Pair<String, String> y) { | |
if (x.snd.equals(y.snd)) { | |
return x.fst.compareTo(y.fst); | |
} | |
else { | |
return x.snd.compareTo(y.snd); | |
} | |
} | |
} |
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
sortLogs(String[] logs) { | |
if (logs == null) { | |
return logs; | |
} | |
if (logs.length < 2) { | |
return logs; | |
} | |
List<String> digitLog = new ArrayList<>(); | |
List<Pair<String, String>> letterLog = new ArrayList<>(); | |
for(String log : logs) { | |
String[] words = log.split(" "); | |
String separatedData = String.join(" ", Arrays.copyOfRange(words, 1, words.length) ); | |
if (separatedData.matches(".*\\d.*")) { | |
digitLog.add(log); | |
} | |
else { | |
Pair<String, String> pair = new Pair<>(words[0], separatedData); | |
letterLog.add(pair); | |
} | |
} | |
Collections.sort(letterLog, (a,b)-> (a.snd).equals(b.snd) ? a.fst.compareTo(b.fst) : a.snd.compareTo(b.snd)); | |
int i = 0; | |
String[] response = new String[logs.length]; | |
for(Pair<String, String> logLetter : letterLog) { | |
StringBuilder sb = new StringBuilder(); | |
sb.append(logLetter.fst).append(" ").append(logLetter.snd); | |
response[i++] = sb.toString(); | |
} | |
for(String digitLetter : digitLog) { | |
response[i++] = digitLetter; | |
} | |
return response; | |
} |
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
sortLogs2(String[] logs) { | |
if (logs == null) { | |
return logs; | |
} | |
if (logs.length < 2) { | |
return logs; | |
} | |
Comparator<Pair<String, String>> comparator = new PairComparator(); | |
PriorityQueue<Pair<String, String>> priorityQueue = new PriorityQueue<>(logs.length, comparator); | |
List<String> digitLog = new ArrayList<>(); | |
for(String log : logs) { | |
int firstBlackSpace = log.indexOf(' '); | |
String logKey = log.substring(0, firstBlackSpace); | |
String separatedData = log.substring(firstBlackSpace + 1); | |
if (separatedData.matches(".*\\d.*")) { | |
digitLog.add(log); | |
} | |
else { | |
Pair<String, String> pair = new Pair<>(logKey, separatedData); | |
priorityQueue.add(pair); | |
} | |
} | |
//Collections.sort(letterLog, (a,b)-> (a.snd).equals(b.snd) ? a.fst.compareTo(b.fst) : a.snd.compareTo(b.snd)); | |
String[] response = new String[logs.length]; | |
int i = 0; | |
while (priorityQueue.size() != 0) { | |
Pair<String, String> logLetter = priorityQueue.remove(); | |
StringBuilder sb = new StringBuilder(); | |
sb.append(logLetter.fst).append(" ").append(logLetter.snd); | |
response[i++] = sb.toString(); | |
} | |
for(String digitLetter : digitLog) { | |
response[i++] = digitLetter; | |
} | |
return response; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment