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

前端 未结 7 1172
情话喂你
情话喂你 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:38

    The awesome, simple way is:

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

    I found it from check the source code of lodash, it always check the function one by one.

    Hope that help.

提交回复
热议问题