I am looking for a function (preferably using lodash, etc.) that will take the array and yield the expected output as follows:
const arr = [{name: 'Anna', score:4}, {name: 'Bob', score:5}, {name: 'Chris', score:4}]
// expected output
// [{name: 'Anna', score:4}, {name: 'Chris', score:4}, {name: 'Bob', score:5}]
i.e. the function should sort by:
1) the frequency of the 'score' property
2) the 'name' property (where scores are equal)
Note that because of (1), this question is not the same as those marked as duplicates.
var _ = require('lodash'); const results = [{name: 'Anna', score:4}, {name: 'Bob', estimateValue:5},{name: 'Chris', estimateValue:4}]; const counts = _.countBy(results, x => x['score']); const r = results.map(x => ({...x, ...{'count': counts[x['score']]}})); _.orderBy(r, 'count')works....orderBy. So_.orderBy(r, ['count', 'name']should work.