How can I use lodash/underscore to sort by multiple nested fields?

前端 未结 7 1158
情话喂你
情话喂你 2021-02-03 21:51

I want to do something like this:

var data = [
    {
        sortData: {a: \'a\', b: 2}
    },
    {
        sortData: {a: \'a\', b: 1}
    },
    {
        sort         


        
相关标签:
7条回答
  • 2021-02-03 22:47

    Update: See the comments below, this is not a good solution in most cases.


    Someone kindly answered in the issue I created. Here's his answer, inlined:

    _.sortBy(data, function(item) {
       return [item.sortData.a, item.sortData.b];
    });
    

    I didn't realize that you're allowed to return an array from that function. The documentation doesn't mention that.

    0 讨论(0)
提交回复
热议问题