Underscore: sortBy() based on multiple attributes

后端 未结 11 1153
隐瞒了意图╮
隐瞒了意图╮ 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 12:07

    I know I'm late to the party, but I wanted to add this for those in need of a clean-er and quick-er solution that those already suggested. You can chain sortBy calls in order of least important property to most important property. In the code below I create a new array of patients sorted by Name within RoomNumber from the original array called patients.

    var sortedPatients = _.chain(patients)
      .sortBy('Name')
      .sortBy('RoomNumber')
      .value();
    

提交回复
热议问题