Underscore: sortBy() based on multiple attributes

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

    Simple Example from http://janetriley.net/2014/12/sort-on-multiple-keys-with-underscores-sortby.html (courtesy of @MikeDevenney)

    Code

    var FullySortedArray = _.sortBy(( _.sortBy(array, 'second')), 'first');
    

    With Your Data

    var FullySortedArray = _.sortBy(( _.sortBy(patients, 'roomNumber')), 'name');
    

提交回复
热议问题