有如下数组:

let arr = [
    {playerid: 1, score: 120, time: '00:24', count: 24},
    {playerid: 2, score: 110, time: '00:16', count: 16},
    {playerid: 3, score: 120, time: '00:24', count: 23},
    {playerid: 4, score: 105, time: '00:15', count: 16},
    {playerid: 4, score: 105, time: '00:18', count: 13}
]

需要先根据 score 降序排序,score 值相同的时候再以 count 降序排序。得出如下结果:

arr = [
    {playerid: 1, score: 120, time: '00:24', count: 24},
    {playerid: 3, score: 120, time: '00:24', count: 23},
    {playerid: 2, score: 110, time: '00:16', count: 16},
    {playerid: 4, score: 105, time: '00:15', count: 16},
    {playerid: 4, score: 105, time: '00:18', count: 13}
]

一行代码解决问题:

arr.sort((x, y) => y.score - x.score || y.count - x.count) 

相关内容:Javascript根据数组中对象的指定属性值找到对象