Skip to content

Commit 2d60df1

Browse files
author
beck chen
committed
add reorder log files
1 parent 9c598c5 commit 2d60df1

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* @param {string[]} logs
3+
* @return {string[]}
4+
*/
5+
6+
/* helper functions */
7+
const logComparator = (logA, logB) => {
8+
// compare body first
9+
const bodyCompareResult = getLogBody(logA).localeCompare(getLogBody(logB));
10+
if (bodyCompareResult !== 0) { return bodyCompareResult; }
11+
12+
return logA.localeCompare(logB);
13+
}
14+
15+
const getLogBody = logStr => logStr.slice(logStr.indexOf(' ') + 1, logStr.length);
16+
const getIsNumLog = logStr => /\d/.test(logStr);
17+
18+
/* end of helper functions */
19+
20+
var reorderLogFiles = function (logs) {
21+
const digitLogs = [], letterLogs = [];
22+
for (log of logs) {
23+
if (getIsNumLog(getLogBody(log))) {
24+
digitLogs.push(log);
25+
} else {
26+
letterLogs.push(log);
27+
}
28+
}
29+
30+
return [...letterLogs.sort(logComparator), ...digitLogs];
31+
};

0 commit comments

Comments
 (0)