Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

175
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda