Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

218
Views
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
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!