题解 | #字符串排序#
字符串排序
https://www.nowcoder.com/practice/5190a1db6f4f4ddb92fd9c365c944584
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
while ((line = await readline())) {
let result = sortString(line);
console.log(result);
}
function sortString(str) {
const list = str.match(/[a-zA-Z]/gi);
const sortList = list.sort((a,b) => {
if (a.toUpperCase() > b.toUpperCase()) {
return 1;
}
if (a.toUpperCase() < b.toUpperCase()) {
return -1;
}
});
let result = "";
for(let i = 0; i < str.length; i++) {
let ch = str.slice(i, i+1);
if (/[a-zA-Z]/i.test(ch)) {
result += sortList.shift();
} else {
result += ch;
}
}
return result;
}
})();

