js
function compare(name, minor) {
return function (o, p) {
var a, b;
if (o && p && typeof o === 'object' && typeof p === 'object') {
a = o[name];
b = p[name];
if (a === b) {
return typeof minor === 'function' ? minor(o, p) : 0;
}
if (typeof a === typeof b) {
return a < b ? -1 : 1;
}
return typeof a < typeof b ? -1 : 1;
} else {
thro("error");
}
}
}
json数组
data: [{
id: '1',
discount: 100,
member_id: 20
},
{
id: '2',
discount: 300,
member_id: 20
},
{
id: '3',
discount: 200,
member_id: 20
},
{
id: '4',
discount: 100,
member_id: 20
}
]
先根据id排列,再根据discount排列
data.sort(compare('id',compare('discount')))