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

150
Visualizações
How to sort array of objects by multiple keys?

I have this array:

let arr = [{
    "guid": 1,
    creationDateTime: 1632180639045,
    "pricePerUnitPerHour": 5,
    "awardedSavingCalculationStatus": 1,
    divisible: true,
}, {
    "guid": 2,
    creationDateTime: 2504091567119,
    "pricePerUnitPerHour": 20,
    "awardedSavingCalculationStatus": 1,
    divisible: true,
}, {
    "guid": 3,
    creationDateTime: 1504095567183,
    "pricePerUnitPerHour": 5,
    "awardedSavingCalculationStatus": 1,
    divisible: true,
}]

I need to make the ascending sorting by these three properties pricePerUnitPerHour, divisible and creationDateTime.

So if for example two objects are having equal values in pricePerUnitPerHour then the sorting shoould be by divisible property. There divisible:true should be before the object where we have divisible:false.

And on the end if they have two equal prices, divisible property is true for example the sorting should be done based on the timestamp - creationDateTime

what i tried

i found some answers at stackoverflow but there he makes the sorting by only two fields but i can't find a way to sort by third one

 arr.sort(
      function (a, b) {
        if (a.pricePerUnitPerHour === b.pricePerUnitPerHour) {
          return b.divisible - a.divisible;
        }
        if(a.divisible === b.divisible) {
            return new Date(a.creationDateTime) - new Date(b.creationDateTime)
        }
        return a.pricePerUnitPerHour > b.pricePerUnitPerHour ? 1 : -1;
      });

Here the sorting works if the prices and the divisible ones are same then it is sorted by them but on the creationDateTime it is not sorting on ascending order

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

0

Sort as follows:

arr.sort(function(a, b) {
    return a.pricePerUnitPerHour - b.pricePerUnitPerHour ||
        -(a.divisible - b.divisible) ||
        a.creationDateTime - b.creationDateTime;
});

The subtraction operation will return 0 if both values are equal (doh) in which case the code moves to the next comparison. The true/false case needs to be inverted since you want 1 (true) to sort before 0 (false).

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