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排序是按照编码排序的)
原文地址:https://www.cnblogs.com/LLLLily/p/10947965.html
时间: 2024-10-10 16:31:36