为了账号安全,请及时绑定邮箱和手机立即绑定

排序多维对象数组

排序多维对象数组

慕后森 2022-08-04 10:16:32
我有多维对象数组,我想按日期排序。下面我添加了我试图做到这一点的方法之一,但是失败了。有没有简单的方法可以在不循环的情况下对其进行排序?let araaaay = [  [{      "dateTime": "2020-01-22T00:00:00",      "date": "2020 01 22",      "hour": 0,      "postsCount": 1,      "shift": "None"    },   ],  [{    "dateTime": "2020-01-19T00:00:00",    "date": "2020 01 19",    "hour": 0,    "postsCount": 2,    "shift": "A"  }, {    "dateTime": "2020-01-19T23:00:00",    "date": "2020 01 19",    "hour": 23,    "postsCount": 2,    "shift": "None"  }],  [{    "dateTime": "2020-01-20T00:00:00",    "date": "2020 01 20",    "hour": 0,    "postsCount": 0,    "shift": "None"  }, {    "dateTime": "2020-01-20T01:00:00",    "date": "2020 01 20",    "hour": 1,    "postsCount": 0,    "shift": "None"  }, {    "dateTime": "2020-01-20T17:00:00",    "date": "2020 01 20",    "hour": 17,    "postsCount": 4,    "shift": "None"  }, {    "dateTime": "2020-01-20T18:00:00",    "date": "2020 01 20",    "hour": 18,    "postsCount": 4,    "shift": "None"  } ],  [{      "dateTime": "2020-01-18T00:00:00",      "date": "2020 01 18",      "hour": 0,      "postsCount": 0,      "shift": "A"    }, {      "dateTime": "2020-01-18T03:00:00",      "date": "2020 01 18",      "hour": 3,      "postsCount": 4,      "shift": "A"    }, {      "dateTime": "2020-01-18T04:00:00",      "date": "2020 01 18",      "hour": 4,      "postsCount": 8,      "shift": "A"    }, {      "dateTime": "2020-01-18T05:00:00",      "date": "2020 01 18",      "hour": 5,      "postsCount": 10,      "shift": "B"    }, {      "dateTime": "2020-01-18T06:00:00",      "date": "2020 01 18",      "hour": 6,      "postsCount": 8,      "shift": "B"    }, {      "dateTime": "2020-01-18T07:00:00",      "date": "2020 01 18",      "hour": 7,      "postsCount": 2,      "shift": "B"    },    {      "dateTime": "2020-01-18T08:00:00",      "date": "2020 01 18",      "hour": 8,      "postsCount": 0,      "shift": "B"    },
查看完整描述

2 回答

?
紫衣仙女

TA贡献1839条经验 获得超15个赞

看起来每个组在同一天都有不同的条目,但时间不同。这意味着你需要两种不同的排序:


对组内的对象进行排序的内部(按 )和对组进行排序的外部排序(按其任何条目的 或 排序):dateTimedatedateTime


const arr = [

  [{

    "dateTime": "2020-01-22T00:00:00",

    "date": "2020 01 22",

    "hour": 0,

    "postsCount": 1,

    "shift": "None"

  }], [{

    "dateTime": "2020-01-19T00:00:00",

    "date": "2020 01 19",

    "hour": 0,

    "postsCount": 2,

    "shift": "A"

  }, {

    "dateTime": "2020-01-19T23:00:00",

    "date": "2020 01 19",

    "hour": 23,

    "postsCount": 2,

    "shift": "None"

  }], [{

    "dateTime": "2020-01-20T00:00:00",

    "date": "2020 01 20",

    "hour": 0,

    "postsCount": 0,

    "shift": "None"

  }, {

    "dateTime": "2020-01-20T01:00:00",

    "date": "2020 01 20",

    "hour": 1,

    "postsCount": 0,

    "shift": "None"

  }, {

    "dateTime": "2020-01-20T17:00:00",

    "date": "2020 01 20",

    "hour": 17,

    "postsCount": 4,

    "shift": "None"

  }, {

    "dateTime": "2020-01-20T18:00:00",

    "date": "2020 01 20",

    "hour": 18,

    "postsCount": 4,

    "shift": "None"

  }], [{

    "dateTime": "2020-01-18T00:00:00",

    "date": "2020 01 18",

    "hour": 0,

    "postsCount": 0,

    "shift": "A"

  }, {

    "dateTime": "2020-01-18T03:00:00",

    "date": "2020 01 18",

    "hour": 3,

    "postsCount": 4,

    "shift": "A"

  }, {

    "dateTime": "2020-01-18T04:00:00",

    "date": "2020 01 18",

    "hour": 4,

    "postsCount": 8,

    "shift": "A"

  }, {

    "dateTime": "2020-01-18T05:00:00",

    "date": "2020 01 18",

    "hour": 5,

    "postsCount": 10,

    "shift": "B"

  }, {

    "dateTime": "2020-01-18T06:00:00",

    "date": "2020 01 18",

    "hour": 6,

    "postsCount": 8,

    "shift": "B"

  }, {

    "dateTime": "2020-01-18T07:00:00",

    "date": "2020 01 18",

    "hour": 7,

    "postsCount": 2,

    "shift": "B"

  }, {

    "dateTime": "2020-01-18T08:00:00",

    "date": "2020 01 18",

    "hour": 8,

    "postsCount": 0,

    "shift": "B"

  }], [{

    "dateTime": "2020-01-21T22:00:00",

    "date": "2020 01 21",

    "hour": 22,

    "postsCount": 0,

    "shift": "None"

  }, {

    "dateTime": "2020-01-21T23:00:00",

    "date": "2020 01 21",

    "hour": 23,

    "postsCount": 0,

    "shift": "None"

  }], [{

    "dateTime": "2020-01-23T00:00:00",

    "date": "2020 01 23",

    "hour": 0,

    "postsCount": 0,

    "shift": "None"

  }, {

    "dateTime": "2020-01-23T07:00:00",

    "date": "2020 01 23",

    "hour": 7,

    "postsCount": 1,

    "shift": "None"

  }, {

    "dateTime": "2020-01-23T21:00:00",

    "date": "2020 01 23",

    "hour": 21,

    "postsCount": 1,

    "shift": "None"

  }],

];



// Inner sort ("by time"):

let sorted = arr.map((group) => {

  return group.sort((a, b) => {

    return new Date(a.dateTime) - new Date(b.dateTime);

  });

});


// Outer sort ("by day"):

sorted = arr.sort((a, b) => {

  return new Date(a[0].dateTime) - new Date(b[0].dateTime);

});


console.log(sorted.map(item => item.map(item => item.dateTime)))

.as-console-wrapper {

  max-height: 100% !important;

}


查看完整回答
反对 回复 2022-08-04
?
哈士奇WWW

TA贡献1799条经验 获得超6个赞

您需要在以下位置使用阵列平面:


const

  araaaay = 

    [ [ { dateTime: '2020-01-22T00:00:00', date: '2020 01 22', hour:  0, postsCount:  1, shift: 'None' } 

      ] 

    , [ { dateTime: '2020-01-19T00:00:00', date: '2020 01 19', hour:  0, postsCount:  2, shift: 'A'    } 

      , { dateTime: '2020-01-19T23:00:00', date: '2020 01 19', hour: 23, postsCount:  2, shift: 'None' } 

      ] 

    , [ { dateTime: '2020-01-20T00:00:00', date: '2020 01 20', hour:  0, postsCount:  0, shift: 'None' } 

      , { dateTime: '2020-01-20T01:00:00', date: '2020 01 20', hour:  1, postsCount:  0, shift: 'None' } 

      , { dateTime: '2020-01-20T17:00:00', date: '2020 01 20', hour: 17, postsCount:  4, shift: 'None' } 

      , { dateTime: '2020-01-20T18:00:00', date: '2020 01 20', hour: 18, postsCount:  4, shift: 'None' } 

      ] 

    , [ { dateTime: '2020-01-18T00:00:00', date: '2020 01 18', hour:  0, postsCount:  0, shift: 'A'    } 

      , { dateTime: '2020-01-18T03:00:00', date: '2020 01 18', hour:  3, postsCount:  4, shift: 'A'    } 

      , { dateTime: '2020-01-18T04:00:00', date: '2020 01 18', hour:  4, postsCount:  8, shift: 'A'    } 

      , { dateTime: '2020-01-18T05:00:00', date: '2020 01 18', hour:  5, postsCount: 10, shift: 'B'   } 

      , { dateTime: '2020-01-18T06:00:00', date: '2020 01 18', hour:  6, postsCount:  8, shift: 'B'    } 

      , { dateTime: '2020-01-18T07:00:00', date: '2020 01 18', hour:  7, postsCount:  2, shift: 'B'    } 

      , { dateTime: '2020-01-18T08:00:00', date: '2020 01 18', hour:  8, postsCount:  0, shift: 'B'    } 

      , { dateTime: '2020-01-21T22:00:00', date: '2020 01 21', hour: 22, postsCount:  0, shift: 'None' } 

      , { dateTime: '2020-01-21T23:00:00', date: '2020 01 21', hour: 23, postsCount:  0, shift: 'None' } 

      ] 

    , [ { dateTime: '2020-01-23T00:00:00', date: '2020 01 23', hour:  0, postsCount:  0, shift: 'None' } 

      , { dateTime: '2020-01-23T07:00:00', date: '2020 01 23', hour:  7, postsCount:  1, shift: 'None' } 

      , { dateTime: '2020-01-23T21:00:00', date: '2020 01 23', hour: 21, postsCount:  1, shift: 'None' } 

      ] ]


const aSorted = araaaay

                  .flat()

                  .sort((a,b)=>new Date(a.dateTime) - new Date(b.dateTime))


console.log (aSorted)

.as-console-wrapper { max-height: 100% !important; }


查看完整回答
反对 回复 2022-08-04
  • 2 回答
  • 0 关注
  • 58 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信