为了账号安全,请及时绑定邮箱和手机立即绑定

如何对每行具有3个值的数组进行排序?

如何对每行具有3个值的数组进行排序?

富国沪深 2021-04-04 16:13:23
这是我必须从最高平均成绩到最低平均成绩排序的数组。let students= [         {name:"Petar", year:1, average:4.35},         {name:"Ivana", year:1, average:3.88},         {name:"Marko", year:2, average:2.27},         {name:"Davor", year:2, average:4.15},         {name:"Petra", year:3, average:3.99},         {name:"Ivan", year:3, average:4.33},         {name:"Goran", year:3, average:3.74}     ];students.sort(function(a,b){         return a[1] - b[1];     });
查看完整描述

1 回答

?
弑天下

TA贡献1818条经验 获得超8个赞

参数a和binsort函数是对象,而不是数组。使用dot访问的对象,而不是括号的财产。


let students= [

    {name:"Petar", year:1, average:4.35},

    {name:"Ivana", year:1, average:3.88},

    {name:"Marko", year:2, average:2.27},

    {name:"Davor", year:2, average:4.15},

    {name:"Petra", year:3, average:3.99},

    {name:"Ivan", year:3, average:4.33},

    {name:"Goran", year:3, average:3.74}

];


const output = students.sort((a, b) => b.average - a.average);


console.log(output);


查看完整回答
反对 回复 2021-04-15
  • 1 回答
  • 0 关注
  • 154 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信