题解 | #字符串排序#
字符串排序
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) {
let input = line.split("");
let char = [];
//let words = line.match(/[a-z]+/ig).join('').split('')
input.forEach((c) => {
// 正则是否为字母
let re = /[a-zA-Z]/;
if (c.match(re)) {
char.push(c);
}
});
char.sort((a,b)=>{
a = a.toLowerCase();
b = b.toLowerCase();
return a<b ? -1: 0
});
let index = 0;
let len = input.length;
// 替代字符串
for (let i = 0; i < len; i++) {
let re = /[a-zA-Z]/;
if (input[i].match(re)) {
input[i] = char[index];
index++;
}
}
console.log(input.join(""))
});