如何根据JavaScript中的时期时间对物体进行排序
我想根据时间安排我从API中获得的数据。 这是数组中的对象之一。
{
"orderId": "xxxxxxxxxxxxx",
"amount": 10,
"status": "CREATED",
"transactionTime": 1651498914,
"type": "ADD",
"mode": "UPI",
"bankAccountId": {
"bankAccountNumber": "xxxxxxxxxxxxx",
"ifsc": "XXXX XXXX XXXX"
}
}
我想根据交易时间以降序排列阵列,这是一个时期的时间。
我的最初方法。
data.map((item) => item.transactionTime)
.sort()
.reverse()
.map((item) => {
return data.find((i) => i.transactionTime == item);
})
此代码非常有效,但是如您所见,这不是这样做的好方法。 因此,我想要一个更优化的解决方案。有人建议使用优先队列。
I want to arrange the data which I am getting from the API according to the time.
Here is one of the objects inside the array.
{
"orderId": "xxxxxxxxxxxxx",
"amount": 10,
"status": "CREATED",
"transactionTime": 1651498914,
"type": "ADD",
"mode": "UPI",
"bankAccountId": {
"bankAccountNumber": "xxxxxxxxxxxxx",
"ifsc": "XXXX XXXX XXXX"
}
}
I want to arrange the array in descending order according to the transactionTime which is an epoch time.
My initial approach.
data.map((item) => item.transactionTime)
.sort()
.reverse()
.map((item) => {
return data.find((i) => i.transactionTime == item);
})
This code is perfectly working but as you can see, it is not a good way to do this.
So I want a more optimized solution. Someone suggested using a priority queue.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以在
sort
回调中直接减去时间。You can directly subtract the times in the
sort
callback.