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;
}
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; }
添加回答
举报