美文网首页
在数组里面嵌套JSON的数据格式,根据key对value进行排序

在数组里面嵌套JSON的数据格式,根据key对value进行排序

作者: 新篇章 | 来源:发表于2018-10-12 11:26 被阅读0次
传参name即为key;
var sortSmall_Big = function(name, minor) {
    return function(o, p) {
        var a, b;
        if(o && p && typeof o === 'object' && typeof p === 'object') {
            a = o[name];
            b = p[name];
            if(a === b) {
                return typeof minor === 'function' ? minor(o, p) : 0;
            }
            if(typeof a === typeof b) {
                return a < b ? -1 : 1;
            }
            return typeof a < typeof b ? -1 : 1;
        } else {
            thro("error");
        }
    }
}

//排序从大到小
var sortBig_Small = function(name, minor) {
    return function(p, o) {
        var a, b;
        if(o && p && typeof o === 'object' && typeof p === 'object') {
            a = o[name];
            b = p[name];
            if(a === b) {
                return typeof minor === 'function' ? minor(o, p) : 0;
            }
            if(typeof a === typeof b) {
                return a < b ? -1 : 1;
            }
            return typeof a < typeof b ? -1 : 1;
        } else {
            thro("error");
        }
    }
}

saveArr.sort(sortSmall_Big(Key));

相关文章

网友评论

      本文标题:在数组里面嵌套JSON的数据格式,根据key对value进行排序

      本文链接:https://www.haomeiwen.com/subject/pbbwaftx.html