有这样一组数据:
const data = [
{
name: '一山',
surname: '张',
adress: '西藏',
area: '北京',
},
{
name: '一山',
surname: '李',
adress: '厦门',
area: '上海',
},
{
name: '尚山',
surname: '王',
adress: '宁夏',
area: '福州',
},
{
name: '一山',
surname: '张',
adress: '福建',
area: '厦门',
},
{
name: '五山',
surname: '张',
adress: '北京',
area: '北京',
},
]
将其按照surname+name+address进行排序,知识点就是String.prototype.localeCompare这个方法,它能根据不同语言进行排序,返回值为-1,0,1,配合sort方法即可解题
const data = [
{
name: '一山',
surname: '张',
adress: '西藏',
area: '北京',
},
{
name: '一山',
surname: '李',
adress: '厦门',
area: '上海',
},
{
name: '尚山',
surname: '王',
adress: '宁夏',
area: '福州',
},
{
name: '一山',
surname: '张',
adress: '福建',
area: '厦门',
},
{
name: '五山',
surname: '张',
adress: '北京',
area: '北京',
},
]
const result = data.sort((a, b) => {
return (a.surname + a.name + a.adress).localeCompare(
b.surname + b.name + b.adress
)
})
console.log(result)