admin管理员组文章数量:1300010
Supposing 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 (a nested sort so).
Result would be:
[
{firstName: 'Thierry', weight: 1},
{firstName: 'John', weight: 2},
{firstName: 'Michael', weight: 2},
{firstName: 'Jordan', weight: 3},
{firstName: 'Steph', weight: 3}
];
I achieve this quickly 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 it using Lodash 2.4.1?
Supposing 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 (a nested sort so).
Result would be:
[
{firstName: 'Thierry', weight: 1},
{firstName: 'John', weight: 2},
{firstName: 'Michael', weight: 2},
{firstName: 'Jordan', weight: 3},
{firstName: 'Steph', weight: 3}
];
I achieve this quickly 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 it using Lodash 2.4.1?
Share Improve this question edited Nov 26, 2015 at 0:10 Igor Raush 15.2k1 gold badge37 silver badges57 bronze badges asked Nov 25, 2015 at 22:58 Mik378Mik378 22.2k15 gold badges91 silver badges190 bronze badges3 Answers
Reset to default 7You can chain two sorts:
_(members).sortBy('firstName').sortBy('weight').value()
It's even in plain Javascript very short.
var members = [
{ firstName: 'Michael', weight: 2 },
{ firstName: 'Thierry', weight: 1 },
{ firstName: 'Steph', weight: 3 },
{ firstName: 'Jordan', weight: 3 },
{ firstName: 'John', weight: 2 }
];
members.sort(function (a, b) {
return a.weight - b.weight || a.firstName.localeCompare(b.firstName);
});
document.write('<pre>' + JSON.stringify(members, 0, 4) + '</pre>');
Another variation of using lodash if you are using an older version of lodash
var members = [
{ firstName: 'Michael', weight: 2 },
{ firstName: 'Thierry', weight: 1 },
{ firstName: 'Steph', weight: 3 },
{ firstName: 'Jordan', weight: 3 },
{ firstName: 'John', weight: 2 }
];
var result = _.sortBy(members, ['firstname', 'weight'], ['asc', 'asc']);
console.log('result ', result);
本文标签: javascriptSort by multiple fields in Lodash 2xStack Overflow
版权声明:本文标题:javascript - Sort by multiple fields in Lodash 2.x - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741641690a2389954.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论