【问题标题】:Sort an array of objects according to two properties values [duplicate]根据两个属性值对对象数组进行排序[重复]
【发布时间】:2020-09-22 19:24:00
【问题描述】:

我有一个对象数组:

let items = [
  { name: 'eric', type: 'comparable',  value: 1 },
  { name: 'bob', type: 'comparable', value: 4 },
  { name: 'michael', type: 'comparable', value: 0 },
  { name: 'john', type: 'comparable', value: 3 },
  { name: 'brad', type: 'incomparable', value: null },
  { name: 'james', type: 'incomparable', value: 5},
  { name: 'martin', type: 'comparable', value: 2 },
  { name: 'chris', type: 'comparable', value: null }
];

我想对我的数组进行排序,以便对象可以按以下方式排序:

  • 首先根据“type”属性->如果是=“Comparable”->根据“value”属性排序

  • 其次,根据“value”属性->如果为null,则放在数组的底部

通过“value”属性,如果为null,则使数组底部的对象,如下所示:

  { name: 'michael', type: 'comparable', value: 0 },
  { name: 'eric', type: 'comparable',  value: 1 },
  { name: 'martin', type: 'comparable', value: 2 },
  { name: 'john', type: 'comparable', value: 3 },
  { name: 'bob', type: 'comparable', value: 4 },
  { name: 'chris', type: 'comparable', value: null },
  { name: 'brad', type: 'incomparable', value: null },
  { name: 'james', type: 'incomparable', value: 5}

我已经这样做了:

items.sort((a, b) => {
    return (a.value===null)-(b.value===null) || +(a.value>b.value)||-(a.ordre<b);
});

但是我总是根据“值”属性进行排序,我希望它首先查找该属性

(我不会使用 loadash)

建议?

【问题讨论】:

  • 您的某些类型是“无与伦比的”。这些如何影响排序逻辑?
  • 您的问题是针对 ES6 还是 ES5 的?如果 ES2016(又名 ES7)的一个特性可以回答你的问题,那会被排除吗?如果没有,请从问题中删除特定于版本的标签。
  • “我希望它首先查找该属性”——我觉得那里缺少一个词。我认为这是“类型”,但认为您最了解。另外,{ name: 'brad', type: 'incomparable', value: null } 不应该是数组中的最后一个值吗?

标签: javascript typescript ecmascript-6 ecmascript-5


【解决方案1】:

您可以对type 进行localeCompare,然后在类型相同的情况下进行布尔短路。在表达式的第二部分,您可以计算 value,将 null 强制转换为 Infinity 以将其移至末尾。

const items = [
  { name: 'eric', type: 'comparable',  value: 1 },
  { name: 'bob', type: 'comparable', value: 4 },
  { name: 'michael', type: 'comparable', value: 0 },
  { name: 'john', type: 'comparable', value: 3 },
  { name: 'brad', type: 'incomparable', value: null },
  { name: 'james', type: 'incomparable', value: 5},
  { name: 'martin', type: 'comparable', value: 2 },
  { name: 'chris', type: 'comparable', value: null }
];

items.sort((a, b) => a.type.localeCompare(b.type)
  || (a.value != null ? a.value : Infinity) - (b.value != null ? b.value : Infinity));

console.log(items);

【讨论】:

    【解决方案2】:

    我个人认为,如果逻辑与您描述的方式相似,则更易于阅读。在此示例中,我尝试将您描述的要求纳入一系列 if 语句而不是单个逻辑表达式:

    let items = [
      { name: 'eric', type: 'comparable',  value: 1 },
      { name: 'bob', type: 'comparable', value: 4 },
      { name: 'michael', type: 'comparable', value: 0 },
      { name: 'john', type: 'comparable', value: 3 },
      { name: 'brad', type: 'incomparable', value: null },
      { name: 'james', type: 'incomparable', value: 5},
      { name: 'martin', type: 'comparable', value: 2 },
      { name: 'chris', type: 'comparable', value: null }
    ];
    
    console.log(items.sort((a, b) => {
      if (a.type === 'comparable' && b.type === 'comparable') {
        if (a.value == null) return 1;
        return a.value - b.value;
      }
      if (a.type === 'comparable') return -1;
      return 1;
    }));

    【讨论】:

      猜你喜欢
      • 2017-07-18
      • 2017-04-06
      • 2021-07-14
      • 2012-10-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-15
      • 1970-01-01
      相关资源
      最近更新 更多