睦牧

博客园 首页 新随笔 联系 订阅 管理

 

引用:函数作为参数

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"));

 

posted on 2018-05-02 17:41  睦牧  阅读(133)  评论(0编辑  收藏  举报