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

136
Vistas
Add an Identifier to an object array that doesn't have one using JavaScript

I am new to StackOverflow so I apologize in advance for not being able to type everything structurally.

I have been trying for some time now to add an identifier to my array to use the .sort() function of javascript to have the array content in alphabetical order. It is also important to not disassociate the amount with the name in the array.

My array contains the following when I use the console.log(result);:

['0.4712', 'ARECIBO']
['0.5041', 'CAGUAS']
['0.4121', 'HUMACAO']
['0.4578', 'MAYAGUEZ']
['0.4785', 'PONCE']
['0.4038', 'SAN JUAN']
['0.4318', 'BAYAMON']

I have read many posts and have seen many users post this type of solution...

result.sort(function(a, b) {
            return b[1] - a[0];
        });
        console.log(result);

Sadly, it does not work for me. I strongly believe that not only is my lack of knowledge an impeding factor, but also my lack of identifier in my array. Therefore, I implore your knowledge to solve this issue. If you can provide a brief explanation as to how the solution works, I would be grateful since it helps me grow and understand what I am doing.

Thank you all!

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

0

var a = [
  ['0.4712', 'ARECIBO'],
  ['0.5041', 'CAGUAS'],
  ['0.4121', 'HUMACAO'],
  ['0.4578', 'MAYAGUEZ'],
  ['0.4785', 'PONCE'],
  ['0.4038', 'SAN JUAN'],
  ['0.4318', 'BAYAMON']
]
var result = a.sort(function(a, b) {
  return a[1].localeCompare(b[1]); //compare 2 strings alphbetically
});
console.log(result);

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