https://blog.csdn.net/weixin_44917045/article/details/111128722

let persons = [
    { id: 1, name: '张三', age: 20 },
    { id: 2, name: '李四', age: 18 },
    { id: 3, name: '王五', age: 16 },
    { id: 4, name: '赵六', age: 40 }
];

// 按照age属性升序
let ascPersons = persons.sort((a, b) => a.age - b.age);
console.log('------------------升序-------------------');
console.log(ascPersons);

// 按照age属性降序
let descPersons = persons.sort((a, b) => b.age - a.age);
console.log('------------------降序-------------------')
console.log(descPersons);
文档更新时间: 2021-10-13 13:04   作者:admin