如何让 Lodash orderBy 只对有数据的记录进行排序?

How to make Lodash orderBy to sort only the records with data?

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

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 排序。我正在使用 Lodash orderBy()。这是我为此编写的排序代码段。

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

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

如果你不必使用 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));

先按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)

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

  const statsAnalysed = data.filter((object) => object.isAnalysed);
  const statsNotAnalysed = data.filter((object) => !object.isAnalysed);

  const sortedData = _.orderBy(statsAnalysed, [element], [direction]).concat(statsNotAnalysed);