前端数组对象名字排序有效方法
按name排序
let obj = [ {name: '学生', id: 1 } , { name: '教师', id: 2 } ,{ name: '偶像', id: 3 }];
obj.sort((a, b) => a.name.localeCompare(b.name, 'zh'));
倒序
let obj = [ {name: '学生', id: 1 } , { name: '教师', id: 2 } ,{ name: '偶像', id: 3 }];
obj.sort((a, b) => b.name.localeCompare(a.name, 'zh'));
按英文顺序排序
let obj = [ {name: 'any', id: 1 } , { name: 'box', id: 2 } ,{ name: 'max', id: 3 }];
obj.sort((a, b) => a.name.localeCompare(b.name, 'en'));