Underscore: sortBy() based on multiple attributes

后端 未结 11 1176
隐瞒了意图╮
隐瞒了意图╮ 2020-11-27 11:09

I am trying to sort an array with objects based on multiple attributes. I.e if the first attribute is the same between two objects a second attribute should be used to comap

11条回答
  •  青春惊慌失措
    2020-11-27 11:49

    Just return an array of properties you want to sort with:

    ES6 Syntax

    var sortedArray = _.sortBy(patients, patient => [patient[0].name, patient[1].roomNumber])
    

    ES5 Syntax

    var sortedArray = _.sortBy(patients, function(patient) { 
        return [patient[0].name, patient[1].roomNumber]
    })
    

    This does not have any side effects of converting a number to a string.

提交回复
热议问题