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



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

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,这样的东西应该可以解决它:

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

orderByisAnalysed优先。

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

最新更新