const arr = [
{ name: 'Jesus' },
{ name: 'Peter' },
{ name: 'Andrew' },
{ name: 'John' }
]
// 对对象数组进行字符串排序
arr.sort((a, b) => {
const x = a.name.toLowerCase()
const y = b.name.toLowerCase()
if (x < y) { return -1 }
if (x > y) { return 1 }
return 0
})
console.log(arr) // [{name: 'Andrew'}, {name: 'Jesus'}, {name: 'John'}, {name: 'Peter'}]
网友评论