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

174
Visualizações
How to sort a deeply sorted array numerically in ascending order?

I am trying to sort each array in an array, however, when I sort each nested array, the arrays are not sorted. This is what I've been doing so far:

let triplets = [ [ 1, -1, 0 ], [ 1, -1, 0 ], [ 1, 0, -1 ] ]

let sortedArrays = triplets.sort((a, b) => a[0] - b[0] || a[1] - b[1])

console.log(sortedArrays) // [ [ 1, -1, 0 ], [ 1, -1, 0 ], [ 1, 0, -1 ] ]

// Expect: [ [-1, 0, 1], [-1, 0, 1], [-1, 0, 1] ]

Obviously, they are not sorted. In the first sub array, 1 does not come before 1. Why is this the case and is there a better way to sort these?

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

0

It looks like you don't need to sort the triplets, you need to sort each subarray - so call .sort on the subarrays, not on the outer array.

let triplets = [ [ 1, -1, 0 ], [ 1, -1, 0 ], [ 1, 0, -1 ] ];
for (const subarr of triplets) {
  subarr.sort((a, b) => a - b);
}
console.log(triplets);

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