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排序是按照编码排序的)
怕什么真理无穷,进一寸有一寸的欢喜。