Categories
Leetcode String

Reorder Log Files

let reorderLogFiles = function(logs) {

let alpha = [],
numbers = [];

const isNumber = (str) => {
const [, content] = str.split(' ');
return content.match(/\d+/);
}

const getBody = (str) => {
return str.substring(str.indexOf(' ') + 1);
}

// separate alpha from numbers
logs.forEach(log => {
if (isNumber(log)) {
numbers.push(log);
}
else {
alpha.push(log);
}
});

let comparator = (x, y) => {
let compareResult = getBody(x).localeCompare(getBody(y));
if (compareResult === 0) {
return x.localeCompare(y);
}
return compareResult;
}

return [...alpha.sort(comparator), ...numbers];
};

Leave a Reply

Your email address will not be published. Required fields are marked *