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

220
Visualizações
why is my data not sorting javascript sort function

I tried making a function called sort and no matter what it won't sort.

I'm sorting an array of javascript objects and using the paramater name that's inputted as the sort criteria, for sorting a table of different values

const sort = (data, ascending, param) => {
    console.log(data, ascending, param);
    let a2 = data.sort((a, b) =>
        ascending ? a[param] - b[param] : b[param] - a[param]
    );
    console.log(a2);
};

console output before and after the sort seems to be the same

[
    {
        "id": 1,
        "first_name": "ralph",
        "last_name": "nader",
        "age": 102,
        "phone": 1234
    },
    {
        "id": 2,
        "first_name": "Turkey",
        "last_name": "Face",
        "age": 43,
        "phone": 4444
    },
    {
        "id": 3,
        "first_name": "Taco",
        "last_name": "Breath",
        "age": 43,
        "phone": 4444
    },
    {
        "id": 4,
        "first_name": "fish",
        "last_name": "Butt",
        "age": 43,
        "phone": 4444
    }
]

[
    {
        "id": 1,
        "first_name": "ralph",
        "last_name": "nader",
        "age": 102,
        "phone": 1234
    },
    {
        "id": 2,
        "first_name": "Turkey",
        "last_name": "Face",
        "age": 43,
        "phone": 4444
    },
    {
        "id": 3,
        "first_name": "Taco",
        "last_name": "Breath",
        "age": 43,
        "phone": 4444
    },
    {
        "id": 4,
        "first_name": "fish",
        "last_name": "Butt",
        "age": 43,
        "phone": 4444
    }
]

here's the html (jsx)

<Col2 onClick={() => sort(users, false, 'first_name')}>First Name</Col2>
about 4 years ago · Juan Pablo Isaza
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