题解 | #字符串排序#
字符串排序
https://www.nowcoder.com/practice/5af18ba2eb45443aa91a11e848aa6723
const rl = require("readline").createInterface({ input: process.stdin }); var iter = rl[Symbol.asyncIterator](); const readline = async () => (await iter.next()).value; void (async function () { const arr = []; while ((line = await readline())) { arr.push(line); } arr.shift(); arr.sort((a, b) => { const compare = (x, y) => { if (x[0] > y[0]) return 1; else if (x[0] < y[0]) return -1; if (x[0] === y[0]) { //相同但是位数少的比较小 if (x.length === 1) return -1; if (y.length === 1) return 1; //如果相同递归比较 return compare(x.slice(1), y.slice(1)); } }; return compare(a, b); }); arr.forEach((item) => { console.log(item); }); })();