我正在尝试按三列对数组进行排序。下面是我的示例数组。有时,该列也可以为空,因此我在排序函数中进行了检查。但是,现在我想先对名称进行排序,然后是薪水,然后是奖金。我怎样才能做到这一点?
let employees = [
{name: 'John', salary: 90000, bonus: 1000},
{name: 'David', salary: 90000, bonus: 1200},
{name: 'Ana', salary: 80000, bonus: 3000}
];
// Code for sorting salary first and then bonus:
function sort(a,b) {
if (a.salary=== null && b.salary=== null ){
return a.bonus- b.bonus;
}
else if (a.salary!== null && b.salary=== null ){
return -1;
}
else if (a.salary=== null && b.salary!== null){
return 1;
}else if (a.salary!== null && b.salary!== null){
return a.salary- b.salary;
}
else {
return 1;
}
}
employees.sort(sort);
console.log(employees);
杨__羊羊
慕丝7291255
繁花不似锦
婷婷同学_
随时随地看视频慕课网APP
相关分类