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

115
Vistas
How to sort through an array primarily with first elements and then if they are same, then with second elements. So x-y axis in ascending order

How do I sort array so that this output

[ [ '6.9', '14.5' ], [ '22.2', '28.9' ] ]

is sorted primarily with x value so 6.9 and 22.5 in the above example and secondarily with y axis, 14.5 and 28.9 in the above example.

this is my code currently to get the output.

const mySet = uniqueArray4(x_y_total);

    mySet.forEach(element => {
        let sumx = 0, sumy = 0;
            for(let i = 0; i < element.length; i++) {
                sumx += element[i][0]/element.length;
                sumy += element[i][1]/element.length;
            }
            
            answer.push([sumy.toFixed(1), sumx.toFixed(1)]);
            });
        
    answer.forEach(element => {
        finalanswer.push(element);
    })

Quite new to js, I searched through google but it's been quite difficult as to how should I implement this.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You can sort based on the 1st entry, and then on the 2nd entry, something like:

// Sorting on 1st param
answer.sort((a,b) => a[0] - b[0]);

// Sorting on 2nd param given then 1st param is sorted, if not, return 0
answer.sort((a,b) => a[0] < b[0] ? a[1] - b[1] : 0);
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