Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

276
Visualizações
How to sort array of objects according to epoch time in JavaScript

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.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You can directly subtract the times in the sort callback.

data.sort((a, b)=>b.transactionTime - a.transactionTime);
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda