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>