按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'));