数组排序

function compare(propertyName) {
return function(object1, object2) {
console.log(object1);
var value1 = object1[propertyName];
var value2 = object2[propertyName];
if (value2 < value1) {
return 1;
} else if (value2 > value1) {
return -1;
} else {
return 0;
}
}
}
let brandList = [
{brandName: 'CBC'},
{brandName: 'BCA'},
{brandName: 'ABC'},
{brandName: 'ABB'}];
console.log(brandList.sort(compare('brandName')));
posted @ 2017-07-07 09:45  daxia11  阅读(118)  评论(0)    收藏  举报