你知道我怎么能按字母顺序排序数组的所有字符串?我正在使用localcompare函数但是当我有两个以相同字母开头的字符串时,该函数不会比较第一个字母后面的下一个字母,只是随机排序。提前致谢。
function solve(input) {
let output = [];
for (element of input) {
let name = element.split(`,`)[0];
if (!output.includes(element)) {
output.push(name);
}
output
.sort((a, b) => a.localeCompare(b))
.sort((a, b, ) => a.length - b.length)
}
return output;
}
let input = ["Ashton", "Kutcher", "Ariel", "Lilly", "Keyden", "Aizen", "Billy", "Braston"]
console.log(solve(input))
我希望这能帮到您,
var arr = ["Ashton","Kutcher","Ariel","Lilly","Keyden","Aizen","Billy","Braston"];
console.log(arr.sort());