字符串的完整字母顺序javascript

时间:2019-03-18 11:13:40

标签: javascript function alphabetical

您知道如何按字母顺序对数组的所有字符串排序吗?我正在使用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))

1 个答案:

答案 0 :(得分:0)

我希望这会对您有所帮助

var arr = ["Ashton","Kutcher","Ariel","Lilly","Keyden","Aizen","Billy","Braston"];
console.log(arr.sort());