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

99
Vistas
Reordering array using .sort with parameters

I have a function to reorder an array with objects inside and I call that function with the object property I want to sort, but I don't know how to set the callback function to use the parameter. I tried use eval() but with no success. It's not working correctly.

function order(param){
  const newOrder = arrayContent.sort((first,second) => (`${first}.${param}` > `${second}.${param}`) ? 1 : -1);
};
order('name')
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

It's pretty easy to implement. You can refer to object either with 'dot notation' e.g. object.name or with help of square brackets e.g. object['name'] where you could put any string inside brackets to refer to certain 'inner field'

let arrayContent = [{number:3,name:'Alice'},{number:2, name:'Maxim'}]; //your array
    let order = param => arrayContent.sort((first,second) => first[param] < second[param]?1:-1);
    
    order('name');
    console.log(arrayContent);
    order('number');
    console.log(arrayContent);

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