function creatCompare(propertyName){
return function (ob1,ob2){
var val1=ob1[propertyName];
var val2=ob2[propertyName];
if(val1<val2){
return -1}
else if (val1>val2){
return 1 }
else {
return 0}
};
}
var data=[{name: 'aaaaa',age:28},{name:'bbbb',age:30}]
data.sort(creatCompare('name'))//按照名字排序
data.sort(creatCompare('age'))//按照年龄排序
简单吧!!