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

178
Vistas
How to sort an array based on the second value with javascript?

I have an array:

[
    [0, 'Please select a Customer'],
    [2072, 'S-Customer'],
    [834, '01-Customer'],
    [709, 'C-Customer'],
    [4217, 'Test'],
    [2074, 'A-Customer']
]

but how can I sort it numeric and alphabetically and still have the same ID which is the first value?? It would be like this:

obs: the first value should not be changed.

[
    [0, 'Please select a Customer'],
    [834, '01-Customer'],
    [2074, 'A-Customer'],
    [709, 'C-Customer'],
    [2072, 'S-Customer'],
    [4217, 'Test']
]
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You could keep the first array in place by looking to index 0 and sort the rest by the value of index 1

const
    array = [[0, 'Please select a Customer'], [2072, 'S-Customer'], [834, '01-Customer'], [709, 'C-Customer'], [4217, 'Test'], [2074, 'A-Customer']];
    

array.sort((a, b) => !b[0] - !a[0] || a[1].localeCompare(b[1]));

console.log(array);
.as-console-wrapper { max-height: 100% !important; top: 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