【问题标题】:How to sort array by 3 columns by Javascript?如何通过 Javascript 按 3 列对数组进行排序?
【发布时间】:2020-09-06 07:27:05
【问题描述】:

我正在尝试按三列对数组进行排序。下面是我的示例数组。有时,该列也可以为空,所以我检查了我的排序函数。但是,现在我想先排序名称,然后是薪水,然后是奖金。我怎样才能做到这一点?

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);

【问题讨论】:

  • 只返回第一个非零比较或最后一个值。

标签: javascript arrays function sorting


【解决方案1】:

有一个名为 lodash 的 npm 包。您可以使用他们的 sortBy 方法实现所需的功能。

const _ = require('lodash');

let employees = [
    {name: 'John', salary: 90000, bonus: 1000},
    {name: 'David', salary: 90000, bonus: 1200},
    {name: 'Ana', salary: 80000, bonus: 3000}
];

const sortedArray = _.sortBy(employees, ['name', 'salary', 'bonus']);

希望对你有帮助。

【讨论】:

  • 假设和使用库并不理想。最好与 OP 核实
【解决方案2】:

只返回第一个非零比较值。 我还确保任何不是数字的东西都会排在最后,但如果你确定你只会得到一个有效的数字或null,那么你可以简化检查。

const employees = [
    {name: 'Ana', salary: null, bonus: 3000},
    {name: 'Ana', salary: 80000, bonus: 3000},
    {name: 'Ana', salary: 70000, bonus: 3000},
    {name: 'Ana', salary: 70000, bonus: 1000}
];

employees.sort((a, b) => {
  return a.name.localeCompare(b.name) ||
    safeNumCompare(a.salary, b.salary) ||
    safeNumCompare(a.bonus, b.bonus);
});

console.log(employees);

function safeNumCompare(a, b) {
  const aValid = isValidNum(a), bValid = isValidNum(b);

  if (!(aValid || bValid)) return 0;
  if (!aValid) return 1;
  if (!bValid) return -1;
  return a - b;
}

function isValidNum(num) {
  return typeof num === 'number' && !isNaN(num);
}

【讨论】:

  • 处理字符串时,使用string.localeCompare。它更安全,你的比较
【解决方案3】:

你可以使用这个逻辑:

  • 排序需要返回一个数值。所以你可以使用算术运算。
  • 您的值可以是null,所以最好使用默认值。
    • 0 用于升序
    • Infinity 降序
  • 对上升执行a - b操作。
  • 对于优先级下降,使用|| 这样如果第一个等式返回 0,它将检查下一个优先级

注意:

上述逻辑将按姓名升序排序,然后是薪水,然后是奖金。因此,如果两个用户的薪水相同,那么他们的奖金将按照这种方式进行检查和排序。

let employees = [{ name: 'John', salary: 90000, bonus: 1000 }, { name: 'David', salary: 90000, bonus: 1200}, { name: 'David',  salary: null,  bonus: 1200}, {  name: 'Ana',  salary: 80000,  bonus: 3000}, {  name: 'Ana',  salary: 80000,  bonus: null }];

function sort(a, b) {
  return (a.name.localeCompare(b.name)) ||
    ((a.salary || Infinity) - (b.salary || Infinity)) ||
    ((a.bonus || 0) - (b.bonus || 0));
}

employees.sort(sort);
console.log(employees);

正如plalx 正确建议的那样

他希望空值排在最后,并且按名称排在最前面。

因此使用Infinity 来默认salary

【讨论】:

  • 他希望空值排在最后,并且按名称排在最前面。
  • @plalx 在这种情况下,我会使用你的 INFINITY 想法
  • 是的,我想使用Infinity,但0 || Infinity === Infinity 所以最后我进行了适当的安全比较。
  • 请注意,由于上述注释,新实现将0 排在最后。只需确保您收到评论通知即可。
  • 这是 OP 的问题。工资可以为0吗?如果是,它与null 有何不同
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-11-28
  • 1970-01-01
  • 2021-09-21
  • 2015-05-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多