var arr=[{a:1},{a:3},{a:2}];
function objSort(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;
}
}
} arr.sort(objSort('a')); //var arr=[{a:1},{a:2},{a:3}];
(上面代码返回的升序,如果想返回降序把“return -1“ 和 “return 1” 跳转一下位置)