数组中的对象排序
学习链接:http://www.cnblogs.com/xljzlw/p/3694861.html
var arr = [{name: "zlw", age: 24}, {name: "wlz", age: 25}];
var compare = function (prop) {
return function (obj1, obj2) {
var val1 = obj1[prop];
var val2 = obj2[prop];
if (!isNaN(Number(val1)) && !isNaN(Number(val2))) {
val1 = Number(val1);
val2 = Number(val2);
}
if (val1 < val2) {
return -1;
} else if (val1 > val2) {
return 1;
} else {
return 0;
}
}
}
console.log(arr.sort(compare));