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

132
Vistas
Sorting Js Array according to pre-defined array of indexes

hope you can help me fix this.

So I have one array with nested arrays:

mainArr: [[Carl, 20, Male],[Mike, 30, Male], [Chloe, 45, Female], ...]

Now this array needs to be sorted along another array containing a set order of indexes from the array above.

indexArr: [2,1,3,...]

Result here being:

sortedArr: [[Mike, 30, Male], [Carl, 20, Male], [Chloe, 45, Female], ...]

I thought about writing a ForEach loop with the second array, something along the lines of:

indexArr.forEach(i){
 sortedArr.push(mainArr[indexArr[i]]);
};

But I'm trying to avoid using loops with this function to keep it fast. I couldn't find anything relevant to my problem in any array documentation. Do you know a way to fill sortedArr or sort mainArr efficiently?

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

0

    indexArr.map(ind => mainArr[ind - 1])
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