sort排序

作者: 前端来入坑 | 来源:发表于2018-09-20 14:06 被阅读4次

    根据数组中某一项进行排序

    var arr = [
        {name:'LL',age:5},
        {name:'XX',age:24},
        {name:'YY',age:16},
        {name:'ZZ',age:10},
        {name:'AA',age:26}
    ];
    
    function compare(value){
        return function(a,b){
            var aaa = a[value];
            var bbb = b[value];
            return aaa - bbb;
        }
    }
    console.log(arr.sort(compare('age')))
    
    image.png

    相关文章

      网友评论

        本文标题:sort排序

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