排序多维对象数组

我有多维对象数组,我想按日期排序。下面我添加了我试图做到这一点的方法之一,但是失败了。有没有简单的方法可以在不循环的情况下对其进行排序?


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"

    },


慕后森
浏览 61回答 2
2回答

紫衣仙女

看起来每个组在同一天都有不同的条目,但时间不同。这意味着你需要两种不同的排序:对组内的对象进行排序的内部(按 )和对组进行排序的外部排序(按其任何条目的 或 排序):dateTimedatedateTimeconst 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;}

哈士奇WWW

您需要在以下位置使用阵列平面: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; }
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript