localeCompare() 将数组元素按文字首字母排序

localeCompare()

//Demo

const workers = [
  {
    name: '张铁柱',
    tel: '18233339999'
  },
  {
    name: '阿龙',
    tel: '18233339999'
  },
  {
    name: '王师傅',
    tel: '18233339999'
  }
];

const result = workers.sort(function (a, b) {
    return a.name.localeCompare(b.name);
});

window.console.log(JSON.stringify(result));

//结果
[
  {"name":"阿龙","tel":"18233339999"},
  {"name":"王师傅","tel":"18233339999"},
  {"name":"张铁柱","tel":"18233339999"}
]

你可能感兴趣的:(localeCompare() 将数组元素按文字首字母排序)