美文网首页
JS对对象进行排序

JS对对象进行排序

作者: 余霜序 | 来源:发表于2019-08-14 12:32 被阅读0次
    // var obj =
        var persons = [
            {name:"lily1",sex:"女",age:18},
            {name:"lily2",sex:"女",age:19},
            {name:"lily3",sex:"女",age:11},
            {name:"lily4",sex:"女",age:28},
            {name:"lily5",sex:"女",age:22},
            ]
        
        //请对数组persons进行按照年龄升序排序
        persons.sort(function (a,b) {
            if (a.age>b.age) {
                return 1;
            }else if(a.age<b.age){
                return -1;
            }else{
                return 0;
            }
        })
        console.log(persons);
    

    相关文章

      网友评论

          本文标题:JS对对象进行排序

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