标签:gets news array turn name bsp compare join ons
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));
标签:gets news array turn name bsp compare join ons
原文地址:http://www.cnblogs.com/hngdlxy143/p/6884335.html