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

184
Visualizações
Sort an array of objects that is the second child of an array - By object date time

How can I sort an [array of objects by date] that is the second child of an array ?

I get the following array structure:

array = [
    ["2022/04/14", [object]],
    ["2022/04/15", [object]],
    ["2022/04/20", [object, object, object]],
    ["2022/04/25", [object, object]],
    ["2022/04/30", [object]]
];

//where each object contain a date property that include time
//for exemple each object get :
{
param1:"someText"
date: Fri Apr 15 2022 18:00:00 GMT+0200
paramX:"someText"
}

For the days that include more than one object, objects are not sorted by time.

I've texted the following method, but can't get success with it

const sortedArray = array.sort((a, b) => {
      return new Date(a[1][1].date).getTime() - new Date(b[1][1].date).getTime();
    });

How can I sort object by time for each day of this array ?

Thank you for your help

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

0

Surely there is a more elegant way to solve this problem but here is my approach.

// Here is the result of sorting the objects
const formattedArray = [];

const sortedArray = array.forEach((item, index) => {
  const sortedObjects = item[1].sort((a, b) => {
    return new Date(a.date) - new Date(b.date);
  });

  formattedArray[index] = [item[0], sortedObjects];
})

You don't need to use .getTime().

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