Underscore: sortBy() based on multiple attributes

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

    You could concatenate the properties you want to sort by in the iterator:

    return [patient[0].roomNumber,patient[0].name].join('|');
    

    or something equivalent.

    NOTE: Since you are converting the numeric attribute roomNumber to a string, you would have to do something if you had room numbers > 10. Otherwise 11 will come before 2. You can pad with leading zeroes to solve the problem, i.e. 01 instead of 1.

提交回复
热议问题