题解 | #成绩排序#
成绩排序
https://www.nowcoder.com/practice/8e400fd9905747e4acc2aeed7240978b
const rl = require("readline").createInterface({ input: process.stdin }); var iter = rl[Symbol.asyncIterator](); const readline = async () => (await iter.next()).value; void (async function () { // Write your code here let inputArr = []; while ((line = await readline())) { inputArr.push(line); } let count = inputArr[0]; let type = inputArr[1]; let totalArr = inputArr.slice(2); //构造以名字和成绩对应的对象作为元素的数组(因为键值可能重复) let mainArr = []; totalArr.forEach((el) => { mainArr.push({ key: el.slice(0, el.indexOf(" ")), value: el.slice(el.indexOf(" ")), }); }); let temArr = []; mainArr.forEach(item =>{ temArr.push(item.value) }) if (type === "0") { //成绩降序排列 temArr.sort((a, b) => b - a); } else if (type === "1") { temArr.sort((a, b) => a - b); } else { } let newArr = [...new Set(temArr)]; newArr.forEach((grade) => { mainArr.forEach((item) => { if (item.value === grade) { console.log(item.key + "" + grade); } }); }); })();