如何对多维数组进行多条件排序?

发布于 2022-09-13 01:27:45 字数 1116 浏览 29 评论 0

对 arr 数组同时按 "time" 字段和 "age" 字段进行降序排列

let arr = [
  {
    time: '2021-02-09',
    data: [
      { name: 12, age: 10, id: 2001 },
      { name: 10, age: 11, id: 2010 },
      { name: 15, age: 34, id: 2008 },
      { name: 13, age: 20, id: 2008 },
    ]
  },
  {
    time: '2021-02-01',
    data: [
      { name: 12, age: 10, id: 2001 },
      { name: 10, age: 20, id: 2010 },
      { name: 15, age: 44, id: 2008 },
      { name: 13, age: 50, id: 2008 },
    ]
  },
  {
    time: '2020-12-12',
    data: [
      { name: 12, age: 11, id: 2001 },
      { name: 10, age: 50, id: 2010 },
      { name: 15, age: 09, id: 2008 },
      { name: 13, age: 18, id: 2008 },
    ]
  },
  {
    time: '2021-06-07',
    data: [
      { name: 12, age: 10, id: 2001 },
      { name: 10, age: 10, id: 2010 },
      { name: 15, age: 24, id: 2008 },
      { name: 13, age: 10, id: 2008 },
    ]
  },
  {
    time: '2020-01-11',
    data: [
      { name: 12, age: 10, id: 2001 },
      { name: 10, age: 10, id: 2010 },
      { name: 15, age: 24, id: 2008 },
      { name: 13, age: 10, id: 2008 },
    ]
  },
]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

牵你手 2022-09-20 01:27:45
var compare = function (prop='',desc=false) {
    return function (obj1, obj2) {
        var val1 = obj1[prop];
        var val2 = obj2[prop];
        if (!isNaN(Number(val1)) && !isNaN(Number(val2))) {
            val1 = Number(val1);
            val2 = Number(val2);
        }
        if(desc) [val1,val2] = [val2,val1];
        if (val1 < val2) {
            return -1;
        } else if (val1 > val2) {
            return 1;
        } else {
            return 0;
        }            
    } 
}
arr.sort(compare('time',true)).forEach(item=>item.data.sort(compare('age',true)));
console.log(JSON.stringify(arr));

结果

[
  {
    "time": "2021-06-07",
    "data": [
      {
        "name": 15,
        "age": 24,
        "id": 2008
      },
      {
        "name": 12,
        "age": 10,
        "id": 2001
      },
      {
        "name": 10,
        "age": 10,
        "id": 2010
      },
      {
        "name": 13,
        "age": 10,
        "id": 2008
      }
    ]
  },
  {
    "time": "2021-02-09",
    "data": [
      {
        "name": 15,
        "age": 34,
        "id": 2008
      },
      {
        "name": 13,
        "age": 20,
        "id": 2008
      },
      {
        "name": 10,
        "age": 11,
        "id": 2010
      },
      {
        "name": 12,
        "age": 10,
        "id": 2001
      }
    ]
  },
  {
    "time": "2021-02-01",
    "data": [
      {
        "name": 13,
        "age": 50,
        "id": 2008
      },
      {
        "name": 15,
        "age": 44,
        "id": 2008
      },
      {
        "name": 10,
        "age": 20,
        "id": 2010
      },
      {
        "name": 12,
        "age": 10,
        "id": 2001
      }
    ]
  },
  {
    "time": "2020-12-12",
    "data": [
      {
        "name": 10,
        "age": 50,
        "id": 2010
      },
      {
        "name": 13,
        "age": 18,
        "id": 2008
      },
      {
        "name": 12,
        "age": 11,
        "id": 2001
      },
      {
        "name": 15,
        "age": 9,
        "id": 2008
      }
    ]
  },
  {
    "time": "2020-01-11",
    "data": [
      {
        "name": 15,
        "age": 24,
        "id": 2008
      },
      {
        "name": 12,
        "age": 10,
        "id": 2001
      },
      {
        "name": 10,
        "age": 10,
        "id": 2010
      },
      {
        "name": 13,
        "age": 10,
        "id": 2008
      }
    ]
  }
]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文