Javascript 数组对象多条件排序
admin 阅读:124 2024-03-02
有如下数组:
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) 声明
1、部分文章来源于网络,仅作为参考。 2、如果网站中图片和文字侵犯了您的版权,请联系1943759704@qq.com处理!



