Objects are unordered in JavaScript. If you need something like an object but ordered, you can use _.pairs
to convert it, then sort the list of pairs.
_.pairs({
"Volvo" : [ "S60", "V40" ],
"Volkswagen" : [ "Polo", "Golf", "Passat" ]
})
Gives:
[
["Volvo", [ "S60", "V40" ]],
["Volkswagen", [ "Polo", "Golf", "Passat" ]]
]
...which you can then sort using _.sortBy
. If you assign the above to cars
, then:
_.sortBy(cars, function(x) { return -x[0].length; });
gives:
[
[ 'Volkswagen', ['Polo', 'Golf', 'Passat' ]],
[ 'Volvo', ['S60', 'V40']]
]