I am trying to create a function where it takes in the key of the object needed to sort the values(in this case 'kills'). I tried using dynamicSort
stated in Sort array of objects by string property value, but I'm just getting the list returned. Any ideas as to what I'm doing wrong here?
const list = [
{
name: 'compass',
kills: 35,
assists: 312
},
{
name: 'another one',
kills: 52,
assists: 32
},
{
name: 'another anothe one',
kills: 12,
assists: 30
}
];
const sortByType = (property) => {
return function (a, b) {
let result;
if (a[property] < b[property]) {
result = -1;
}
if (a[property] > b[property]) {
result = 1;
}
else {
result = 0;
}
return result;
};
}
let newList = list.sort(sortByType('kills'));
console.log(newList);
EDITED: updated how the code is being used.