js对对象数组的某一字段排序(前端网备份)

var friends = [

{avg1: 21, avg2: 95.2719, avg3: 0, avg4: 0, college: "经济与管理学院",},
{avg1: 28, avg2: 91.2719, avg3: 0, avg4: 0, college: "经济与管理学院",},
{avg1: 1, avg2: 97.2719, avg3: 0, avg4: 0, college: "经济与管理学院",},
];
var x = "avg1";
//之前出过的bug是a.x(是underfined)原因在a.x会把x当字符串解析,所以得a[x]
friends.sort(function (a, b) {
console.log(a[x])
if (a[x] < b[x]) {
return -1
}
if (a[x] > b[x]) {
return 1
}
return 0;
});
console.log(friends)

上一篇:13、mybatis一对多resultMap中用collection指定集合的封装规则


下一篇:78. Subsets