I have an array of arrays, each nested array contains an object with 2 values: good and bad.
[
0:[
count:{
good: 5,
bad: 3
}
],
1:[
count:{
good: 7,
bad: 6
}
],
2:[
count:{
good: 0,
bad: 8
}
],
3:[
count:{
good: 0,
bad: 9
}
],
4:[
count:{
good: 4,
bad: 1
}
]
]
I'd like to order the array in descending order from high to low by good and then when good is 0, order the rest of arrays in descending order from high to low by bad. So the resulting array would look like this:
[
0:[
count:{
good: 7,
bad: 6
}
],
1:[
count:{
good: 5,
bad: 3
}
],
2:[
count:{
good: 4,
bad: 1
}
],
3:[
count:{
good: 0,
bad: 9
}
],
4:[
count:{
good: 0,
bad: 8
}
]
]
I'm using Underscore and am a little confused as to how to best approach this as there are two values I want to sort by and they are within an object within the nested arrays.
Using sortBy seems to be the obvious choice, but I'm not sure how to first sort by good and then by bad.
sortBy
_.sortBy(list, iterator, [context])
Returns a sorted copy of list, ranked in ascending order by the results of running each value through iterator. Iterator may also be the string name of the property to sort by (eg.
length
).