Assume this array:
var members = [
{firstName: 'Michael', weight: 2},
{firstName: 'Thierry', weight: 1},
{firstName: 'Steph', weight: 3},
{firstName: 'Jordan', weight: 3},
{firstName: 'John', weight: 2}
];
I want to sort by weight and for each kind of weight, sorting by firstNames (nested type).
Result:
[
{firstName: 'Thierry', weight: 1},
{firstName: 'John', weight: 2},
{firstName: 'Michael', weight: 2},
{firstName: 'Jordan', weight: 3},
{firstName: 'Steph', weight: 3}
];
I quickly achieve this using Lodash 2.4.1:
return _.chain(members)
.groupBy('weight')
.pairs()
.sortBy(function(e) {
return e[0];
})
.map(function(e){
return e[1];
})
.map(function(e){
return _.sortBy(e, 'firstName')
})
.flatten()
.value();
Is there a better way to achieve this using Lodash 2.4.1?
source
share