根据某一属性对数组中的对象进行排序

var data=[{name:"za",age:28},{name:"ni",age}];

data.sort(compareFunction("name"));

function compareFunction(propwrtyName){

return function(object1,object2){

var value1=object1[propertyName];

var value2=object2[propertyName];

if(value1

return -1}

else if(value1>value2){

return 1}

else{return 0}

};

}

你可能感兴趣的:(Javascript)