有如下数组:
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)
想要了解更多内容,请持续关注码农资源网,一起探索发现编程世界的无限可能!
本站部分资源来源于网络,仅限用于学习和研究目的,请勿用于其他用途。
如有侵权请发送邮件至1943759704@qq.com删除
码农资源网 » Javascript 数组对象多条件排序
本站部分资源来源于网络,仅限用于学习和研究目的,请勿用于其他用途。
如有侵权请发送邮件至1943759704@qq.com删除
码农资源网 » Javascript 数组对象多条件排序