引用:函数作为参数
var fn_by = function(id) {
return function(o, p) {
var a, b;
if (typeof o === "object" && typeof p === "object" && o && p) {
a = o[id];
b = p[id];
if (a === b) {
return 0;
}
if (typeof a === typeof b) {
return a > b ? -1 : 1; //降序使用 “>”, 升序使用“<”
}
return typeof a > typeof b ? -1 : 1; //降序使用“>”, 升序使用“<”
} else {
throw ("error");
}
}
};
arrayObj.sort(fn_by("id"));