数组排序

it2022-05-09  27

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')));

转载于:https://www.cnblogs.com/znj211985211/p/7130507.html


最新回复(0)