Array.sort()对象数组排序

   var member3 = [
                {age: 4, name: 'Lily3'},
                {age: 1, name: 'Lily2'},
                {age: 10, name: 'Lily1'},
                {age: -12, name: 'dsjf'},
                {age: 21, name: '哈哈'},
            ];
            console.log(member3.sort(function(a,b){
                return a.age - b.age;
            }))

结果如下

 

 

   var member3 = [
                {age: 4, name: 'Lily3'},
                {age: 1, name: 'Lily2'},
                {age: 10, name: 'Lily1'},
                {age: -12, name: 'dsjf'},
                {age: 21, name: '哈哈'},
            ];
            console.log(member3.sort(function(a,b){
                return b.age - a.age;
            }))

结果如下

 

仅适用于排序标准值都为Number类型时(因为sort排序是按照编码排序的)

 

posted @ 2019-05-30 10:06 LLLLily 阅读(...) 评论(...) 编辑 收藏