【问题标题】:How to make Lodash orderBy to sort only the records with data?如何让 Lodash orderBy 只对有数据的记录进行排序?
【发布时间】:2021-08-22 05:12:03
【问题描述】:

我有一个包含以下元素的数组,

let stats = [
  { 'keyword': 'testing',                'searches': 0,    'isAnalysed': false },
  { 'keyword': 'testing software',       'searches': 235,  'isAnalysed': true },
  { 'keyword': 'the testing',            'searches': 0,    'isAnalysed': false },
  { 'keyword': 'testing the boundries',  'searches': 0,    'isAnalysed': false },
  { 'keyword': 'testing business ideas', 'searches': 155,  'isAnalysed': true },
  { 'keyword': 'testing electronics',    'searches': 0,    'isAnalysed': false },
  { 'keyword': 'testing and assessment', 'searches': 0,    'isAnalysed': false },
  { 'keyword': 'testing microservices',  'searches': 65,   'isAnalysed': true },
];

我想将数组排序为如下所示,

升序;

let stats = [
  { 'keyword': 'testing microservices',  'searches': 65,   'isAnalysed': true },
  { 'keyword': 'testing business ideas', 'searches': 155,  'isAnalysed': true },
  { 'keyword': 'testing software',       'searches': 235,  'isAnalysed': true },
  { 'keyword': 'testing',                'searches': 0,    'isAnalysed': false },
  { 'keyword': 'the testing',            'searches': 0,    'isAnalysed': false },
  { 'keyword': 'testing the boundries',  'searches': 0,    'isAnalysed': false },
  { 'keyword': 'testing electronics',    'searches': 0,    'isAnalysed': false },
  { 'keyword': 'testing and assessment', 'searches': 0,    'isAnalysed': false },
];

降序;

let stats = [
  { 'keyword': 'testing software',        'searches': 235,  'isAnalysed': true },
  { 'keyword': 'testing business ideas',  'searches': 155,  'isAnalysed': true },
  { 'keyword': 'testing microservices',   'searches': 65,   'isAnalysed': true },
  { 'keyword': 'testing',                 'searches': 0,    'isAnalysed': false },
  { 'keyword': 'the testing',             'searches': 0,    'isAnalysed': false },
  { 'keyword': 'testing the boundries',   'searches': 0,    'isAnalysed': false },
  { 'keyword': 'testing electronics',     'searches': 0,    'isAnalysed': false },
  { 'keyword': 'testing and assessment',  'searches': 0,    'isAnalysed': false },
];

是的,如果该对象为isAnalysed = true,我想按searches 排序。我正在使用LodashorderBy()。这是我为此编写的排序代码段。

  // direction - asc/desc
  const sortedData = _.orderBy(stats, [function (object) {
    if (object.isAnalysed === true) return object.searches;
  }], [direction]);

但这会像searches 完成的普通排序一样对整个数组进行排序。我在这里错过了什么?

【问题讨论】:

  • 你必须使用 lodash 吗? filtersortconcat 应该可以解决它。

标签: javascript sorting lodash


【解决方案1】:

如果你不必使用 lodash,这样的东西应该可以解决:

const stats = [
  { 'keyword': 'testing software',        'searches': 235,  'isAnalysed': true },
  { 'keyword': 'testing business ideas',  'searches': 155,  'isAnalysed': true },
  { 'keyword': 'testing microservices',   'searches': 65,   'isAnalysed': true },
  { 'keyword': 'testing',                 'searches': 0,    'isAnalysed': false },
  { 'keyword': 'the testing',             'searches': 0,    'isAnalysed': false },
  { 'keyword': 'testing the boundries',   'searches': 0,    'isAnalysed': false },
  { 'keyword': 'testing electronics',     'searches': 0,    'isAnalysed': false },
  { 'keyword': 'testing and assessment',  'searches': 0,    'isAnalysed': false },
];
const statsAnalysed = stats.filter(stat => stat.isAnalysed);
const statsNotAnalysed = stats.filter(stat => !stat.isAnalysed);

const statsAnalysedSorted = (order) => statsAnalysed.sort((s1, s2) => order === 'asc' ? s1.searches - s2.searches : s2.searches - s1.searches);

console.log(statsAnalysedSorted('asc').concat(statsNotAnalysed));

【讨论】:

    【解决方案2】:

    先订购isAnalysed

    orderBy(stats, ['isAnalysed', 'searches'], ['desc', 'desc']))
    orderBy(stats, ['isAnalysed', 'searches'], ['desc', 'asc']))
    

    常规Array#sort比较函数。

    function compareAsc(a, b){
      if (a.isAnalysed === false && b.isAnalysed === false) return 0
      if (a.isAnalysed === false && b.isAnalysed === true) return 1
      if (a.isAnalysed === true && b.isAnalysed === false) return -1  
      return a.searches - b.searches
    }
    function compareDesc(a, b){
      if (a.isAnalysed === false && b.isAnalysed === false) return 0
      if (a.isAnalysed === false && b.isAnalysed === true) return 1
      if (a.isAnalysed === true && b.isAnalysed === false) return -1    
      return b.searches - a.searches
    }
    stats.sort(compareAsc)
    stats.sort(compareDesc)
    

    【讨论】:

      【解决方案3】:

      我通过这两个建议的答案找到了可以使用的方法。首先,我根据已分析和未分析将数据集分为两部分。然后我对分析的数据集进行排序,并将其与未分析的数据集连接起来。

        const statsAnalysed = data.filter((object) => object.isAnalysed);
        const statsNotAnalysed = data.filter((object) => !object.isAnalysed);
      
        const sortedData = _.orderBy(statsAnalysed, [element], [direction]).concat(statsNotAnalysed);
      

      【讨论】:

        猜你喜欢
        • 2021-10-13
        • 2021-08-21
        • 1970-01-01
        • 2016-10-17
        • 1970-01-01
        • 2019-11-25
        • 2018-09-17
        • 1970-01-01
        • 2018-01-28
        相关资源
        最近更新 更多