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.
You can directly subtract the times in the sort
callback.
data.sort((a, b)=>b.transactionTime - a.transactionTime);