I have this array, which has already been sorted by uploadTime
[
{ id: 1133, userId: 101697, uploadTime: '2020-09-14T16:19:16.000Z' },
{ id: 1132, userId: 101543, uploadTime: '2020-09-14T16:17:27.000Z' },
{ id: 1131, userId: 101697, uploadTime: '2020-09-14T16:09:49.000Z' },
{ id: 1130, userId: 101697, uploadTime: '2020-09-14T16:09:16.000Z' },
{ id: 1128, userId: 101543, uploadTime: '2020-09-09T21:48:46.000Z' },
{ id: 1127, userId: 101543, uploadTime: '2020-09-09T21:41:41.000Z' },
{ id: 1126, userId: 101543, uploadTime: '2020-09-09T21:35:37.000Z' },
{ id: 1125, userId: 101543, uploadTime: '2020-09-09T19:41:57.000Z' },
{ id: 1082, userId: 101577, uploadTime: '2020-08-21T15:24:35.000Z' },
{ id: 1049, userId: 101589, uploadTime: '2020-08-15T23:31:42.000Z' },
{ id: 1040, userId: 101589, uploadTime: '2020-08-14T15:04:01.000Z' },
{ id: 1036, userId: 101589, uploadTime: '2020-08-13T00:09:43.000Z' },
{ id: 1035, userId: 101527, uploadTime: '2020-08-12T20:07:34.000Z' },
{ id: 1034, userId: 101612, uploadTime: '2020-08-12T17:33:26.000Z' },
{ id: 996, userId: 101589, uploadTime: '2020-08-11T15:20:11.000Z' },
{ id: 889, userId: 101626, uploadTime: '2020-08-06T14:47:18.000Z' },
{ id: 864, userId: 101589, uploadTime: '2020-08-05T15:23:50.000Z' },
{ id: 863, userId: 101589, uploadTime: '2020-08-05T14:42:14.000Z' },
{ id: 852, userId: 101589, uploadTime: '2020-08-04T18:05:07.000Z' },
{ id: 851, userId: 101589, uploadTime: '2020-08-04T17:57:58.000Z' }
]
Now I want to resort it, but only for objects with the same userId and uploadTime is less than 24 hours from current time. The output should be something like this:
[
{ id: 1133, userId: 101697, uploadTime: '2020-09-14T16:19:16.000Z' },
{ id: 1131, userId: 101697, uploadTime: '2020-09-14T16:09:49.000Z' },
{ id: 1130, userId: 101697, uploadTime: '2020-09-14T16:09:16.000Z' },
{ id: 1132, userId: 101543, uploadTime: '2020-09-14T16:17:27.000Z' },
{ id: 1128, userId: 101543, uploadTime: '2020-09-09T21:48:46.000Z' },
{ id: 1127, userId: 101543, uploadTime: '2020-09-09T21:41:41.000Z' },
{ id: 1126, userId: 101543, uploadTime: '2020-09-09T21:35:37.000Z' },
{ id: 1125, userId: 101543, uploadTime: '2020-09-09T19:41:57.000Z' },
{ id: 1082, userId: 101577, uploadTime: '2020-08-21T15:24:35.000Z' },
{ id: 1049, userId: 101589, uploadTime: '2020-08-15T23:31:42.000Z' },
{ id: 1040, userId: 101589, uploadTime: '2020-08-14T15:04:01.000Z' },
{ id: 1036, userId: 101589, uploadTime: '2020-08-13T00:09:43.000Z' },
{ id: 1035, userId: 101527, uploadTime: '2020-08-12T20:07:34.000Z' },
{ id: 1034, userId: 101612, uploadTime: '2020-08-12T17:33:26.000Z' },
{ id: 996, userId: 101589, uploadTime: '2020-08-11T15:20:11.000Z' },
{ id: 889, userId: 101626, uploadTime: '2020-08-06T14:47:18.000Z' },
{ id: 864, userId: 101589, uploadTime: '2020-08-05T15:23:50.000Z' },
{ id: 863, userId: 101589, uploadTime: '2020-08-05T14:42:14.000Z' },
{ id: 852, userId: 101589, uploadTime: '2020-08-04T18:05:07.000Z' },
{ id: 851, userId: 101589, uploadTime: '2020-08-04T17:57:58.000Z' }
]
Edit: I end up do it like this, a bit longer than the other approach:
let dict = {}
let dictArr = []
let resultGroup = []
let resultNoGroup = []
temp.forEach((item) => {
if (!dict[item.userId]) {
dict[item.userId] = []
}
dict[item.userId].push(item)
dict[item.userId].sort((a, b) => a.uploadTime - b.uploadTime)
})
for (let key in dict) {
dictArr.push(dict[key])
}
dictArr.sort((a, b) => (a[0].uploadTime > b[0].uploadTime ? -1 : 1))
for (let i = 0; i < dictArr.length; i++) {
for (let j = 0; j < dictArr[i].length; j++) {
const timeDiff = Math.abs(
Math.round(
(new Date().getTime() -
new Date(dictArr[i][j].uploadTime).getTime()) /
(1000 * 3600)
)
)
if (timeDiff <= 24) {
resultGroup.push(dictArr[i][j])
} else {
resultNoGroup.push(dictArr[i][j])
}
}
}
resultNoGroup.sort((a, b) => (a.uploadTime > b.uploadTime ? -1 : 1))
const result = [...resultGroup, ...resultNoGroup]
console.log(result)
This work for me as expected!