题解 | #字符串排序#
字符串排序
https://www.nowcoder.com/practice/5190a1db6f4f4ddb92fd9c365c944584
const readline = require("readline");
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
rl.on("line", function (line) {
handle(line);
});
function handle(line) {
const letterLine = line.replace(/[^A-Za-z]/g, "");
let letterLineArr = [...letterLine];
letterLineArr.sort((next, pre) => {
return next.toUpperCase().charCodeAt() - pre.toUpperCase().charCodeAt();
});
const lineArr = [...line];
lineArr.forEach((item, index) => {
if (!isLetter(item)) {
letterLineArr.splice(index, 0, item);
}
});
console.log(letterLineArr.join(""));
}
function isLetter(code) {
return /[A-Za-z]/.test(code);
}