我正在尝试创建一个函数,它接受对值进行排序所需的对象的键(在本例中为“kills”)。我尝试使用按字符串属性值对对象数组进行排序dynamicSort
中所述,但我只是得到返回的列表。关于我在这里做错了什么有什么想法吗?
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);
拉风的咖菲猫
天涯尽头无女友
相关分类