Javascript: sort() PUBLIC SERVICE ANNOUNCEMENT
Hey guys, I spent 2 hours working on this "easy" problem because I made an incorrect assumption about Javascript\'s native sort() method. More specifically, it 
Hey guys, I spent 2 hours working on this "easy" problem because I made an incorrect assumption about Javascript\'s native sort() method. More specifically, it has to do with LeetCode using Node V10.x. The issue is that sort() is NOT stable in that version yet. \n\nAnd so, the below code fails on LeetCode but works on current Chrome V70 console (because Chrome now uses stable sort()). The problem is with:\n```\nreturn isDigitA ? (isDigitB ? 0 : 1) : -1;\n```\n```\nvar reorderLogFiles = function(logs) {\n    function isNum(char) {\n        char = char.codePointAt(0);\n        let nine = \'9\'.codePointAt(0);\n        \n        return char <= nine;\n    }\n    \n    function split(s) {\n        let firstSpaceIdx = s.indexOf(\' \');\n        let identifier = s.slice(0, firstSpaceIdx);\n        let other = s.slice(firstSpaceIdx + 1);\n        \n        return [identifier, other];\n    }\n    \n    logs.sort((a,b) => {\n        let A = split(a);\n        let B = split(b);\n        let isDigitA = isNum(A[1][0]);\n        let isDigitB = isNum(B[1][0]);\n        \n        if(!isDigitA && !isDigitB) {\n            if(A[1] === B[1]) {\n                return A[0] > B[0] ? 1 : -1;\n            } else {\n                return A[1] > B[1] ? 1 : -1;\n            }\n        } else {\n            return isDigitA ? (isDigitB ? 0 : 1) : -1;\n        }\n    });\n    \n    return logs;\n};\n```\n\nBelow is my solution so that it passes on LeetCode.\n\n```\nvar reorderLogFiles = function(logs) {\n    function isNum(char) {\n        char = char.codePointAt(0);\n        let nine = \'9\'.codePointAt(0);\n        \n        return char <= nine;\n    }\n    \n    function split(s) {\n        let firstSpaceIdx = s.indexOf(\' \');\n        let identifier = s.slice(0, firstSpaceIdx);\n        let other = s.slice(firstSpaceIdx + 1);\n        \n        return [identifier, other];\n    }\n    \n    let letters = [];\n    let digits = [];\n    \n    logs.forEach(log=>{\n        if(isNum(split(log)[1][0]))\n            digits.push(log);\n        else\n            letters.push(log);\n    });\n    \n    letters.sort((a,b) => {\n        let A = split(a);\n        let B = split(b);\n        \n        if(A[1] === B[1]) {\n            return A[0] > B[0] ? 1 : -1;\n        } else {\n            return A[1] > B[1] ? 1 : -1;\n        }\n        \n    });\n    \n    return letters.concat(digits);\n};\n```
[]