1

I've done lodash _.countBy and was returned an object like so...

sponsorCounts = { 300051: 2, 300055: 1, 300064: 6 }

Now I'd like to do a sort/order by descending value but all the examples I've found involve pushing data into a separate array to sort like so...

var sortable = [];
for (var sponsor in sponsorCounts)
     sortable.push([sponsor, sponsorCounts[sponsor]])
     sortable.sort(
         function(a, b) {
              return b[1] - a[1]
         }
     )

Which gives me an array... but I want to keep it in that original object so I can get it like...

sponsorCounts = { 300064: 6, 300051: 2, 300055: 1  }
Layne
  • 642
  • 1
  • 13
  • 32
  • 1
    possible duplicate: http://stackoverflow.com/questions/1069666/sorting-javascript-object-by-property-value – roger Oct 07 '16 at 20:26
  • Not a duplicate because I want to keep the object, not create a whole new array. – Layne Oct 07 '16 at 20:42

1 Answers1

0

You can't sort javascript objects. They don't guarantee order. You'll have to use something like an array. Sorry!

Big Money
  • 9,139
  • 6
  • 26
  • 37