Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

107
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda