如何根据JavaScript中的时期时间对物体进行排序

发布于 2025-02-07 19:04:47 字数 636 浏览 2 评论 0原文

我想根据时间安排我从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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

以可爱出名 2025-02-14 19:04:47

您可以在sort回调中直接减去时间。

data.sort((a, b)=>b.transactionTime - a.transactionTime);

You can directly subtract the times in the sort callback.

data.sort((a, b)=>b.transactionTime - a.transactionTime);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文