var staffs=[{name:"join",age:20},{name:"ajax",age:21},{name:"trim",age:22}];
function compare(property){
return function(obj1,obj2){
var value1=obj1[property];
var value2=obj2[property]
return value1-value2;
}
}
var newStaffs=staffs.sort(compare("name"));
console.log(newStaffs)
var array = [
{name: 'a', phone: 1},
{name: 'a', phone: 2},
{name: 'b', phone: 5},
{name: 'd', phone: 3},
{name: 'c', phone: 4}
]
array.sort(getSortFun('desc', 'name'));
function getSortFun(order, sortBy) {
var ordAlpah = (order == 'asc') ? '>' : '<';
var sortFun = new Function('a', 'b', 'return a.' + sortBy + ordAlpah + 'b.' + sortBy + '?1:-1');
return sortFun;
}
console.log(JSON.stringify(array));