做到双重排序时,原数据如下,需要对star和whether进行排序,在做项目时,star代表星级,在星级从大到小的情况下在再对whether进行排序, 代码如下:
let tableData = [
{
date: '04',
name: '王小虎',
address: '上海市普陀区金沙江路 1516 弄',
star: 5,
whether: 1
},
{
date: '05',
name: '王小虎',
address: '上海市普陀区金沙江路 1516 弄',
star: 5,
whether: 1
},
{
date: '06',
name: '王小虎',
address: '上海市普陀区金沙江路 1516 弄',
star: 5,
whether: 0
},
{
date: '07',
name: '王小虎',
address: '上海市普陀区金沙江路 1516 弄',
star: 5,
whether: 1
},
{
date: '08',
name: '王小虎',
address: '上海市普陀区金沙江路 1516 弄',
star: 5,
whether: 0
},
{
date: '09',
name: '王小虎',
address: '上海市普陀区金沙江路 1516 弄',
star: 5,
whether: 1
},
{
date: '11',
name: '王小虎',
address: '上海市普陀区金沙江路 1516 弄',
star: 3,
whether: 0
},
{
date: '12',
name: '王小虎',
address: '上海市普陀区金沙江路 1516 弄',
star: 3,
whether: 0
},
{
date: '13',
name: '王小虎',
address: '上海市普陀区金沙江路 1516 弄',
star: 3,
whether: 1
},
{
date: '14',
name: '王小虎',
address: '上海市普陀区金沙江路 1516 弄',
star: 3,
whether: 1
},
{
date: '15',
name: '王小虎',
address: '上海市普陀区金沙江路 1516 弄',
star: 3,
whether: 1
},
{
date: '16',
name: '王小虎',
address: '上海市普陀区金沙江路 1516 弄',
star: 3,
whether: 0
},
{
date: '17',
name: '王小虎',
address: '上海市普陀区金沙江路 1516 弄',
star: 3,
whether: 0
},
{
date: '18',
name: '王小虎',
address: '上海市普陀区金沙江路 1516 弄',
star: 3,
whether: 1
},
{
date: '19',
name: '王小虎',
address: '上海市普陀区金沙江路 1516 弄',
star: 3,
whether: 1
},
{
date: '20',
name: '王小虎',
address: '上海市普陀区金沙江路 1516 弄',
star: 3,
whether: 0
},
{
date: '01',
name: '王小虎',
address: '上海市普陀区金沙江路 1518 弄',
star: 2,
whether: 0
},
{
date: '02',
name: '王小虎',
address: '上海市普陀区金沙江路 1517 弄',
star: 2,
whether: 0
},
{
date: '03',
name: '王小虎',
address: '上海市普陀区金沙江路 1519 弄',
star: 2,
whether: 1
},
{
date: '10',
name: '王小虎',
address: '上海市普陀区金沙江路 1516 弄',
star: 5,
whether: 1
}
]
tableData.sort((x, y) => {
// 如果星级相同进行 whether 之间的排序
if (x.star === y.star) {
return y.whether - x.whether
} else {
//星级不同时直接以星级进行排序
return y.star - x.star
}
})