0

So I have an array of objects, and I am using underscore.js' _.groupBy function for a particular case. How do I get the output of _.groupBy sorted reverse by keys?

I have this ----

stooges = [
  {name: 'moe', age: 40}, 
  {name: 'larry', age: 50}, 
  {name: 'curly', age: 60}, 
  {name: 'moe', age: 40}, 
  {name: 'larry', age: 50}, 
  {name: 'curly', age: 60}
]

groupBy gives me this ---

_.groupBy(stooges, 'age');
=> {
  "40": [{name: 'moe', age: 40}, {name: 'moe', age: 40}],
  "50": [{name: 'larry', age: 50}, {name: 'larry', age: 50}],
  "60": [{name: 'curly', age: 60}, {name: 'curly', age: 60}]
 }

I want groupBy to give me this ---

=> {
      "60": [{name: 'curly', age: 60}, {name: 'curly', age: 60}]
      "50": [{name: 'larry', age: 50}, {name: 'larry', age: 50}],
      "40": [{name: 'moe', age: 40}, {name: 'moe', age: 40}],

   }
Kishor Pawar
  • 3,386
  • 3
  • 28
  • 61

1 Answers1

0

The keys in an object do not have a well-defined order - the order could change from execution to execution, or from one JavaScript engine to another. (It probably doesn't, but if your code depended on it it could break at any time.)

If you want to enumerate them in a particular order, use _.keys to get an array of the keys in the result object, sort that array and enumerate the array, grabbing the corresponding group out of the result object.

Jesper
  • 7,477
  • 4
  • 40
  • 57
  • I don't want to sort the group, I want a group to be sorted by age. I observed `_.groupBy` sorts group by keys in ascending order. – Kishor Pawar May 21 '18 at 13:14
  • @KishorPawar: The result of `_.groupBy` returns an object and why do you care the sequencing of properties in an object? – Isaac May 21 '18 at 13:17
  • 1
    You get an object from groupBy. The order the keys of a JavaScript object come in is not well-defined and there's no way to re-order them. If you want something that is explicitly ordered a certain way, you will have to turn it into something other than an object. – Jesper May 21 '18 at 13:18
  • @Isaac because that's the business case. – Kishor Pawar May 21 '18 at 13:19
  • @KishorPawar: Perhaps you can have a look at this link to understand more: https://stackoverflow.com/questions/5525795/does-javascript-guarantee-object-property-order – Isaac May 21 '18 at 13:19
  • The business case does not overturn how the language actually works - please see Isaac's link. To fulfill the business case, you will have to use something that has a defined order that you can affect - ie something other than an object. – Jesper May 21 '18 at 13:20
  • @Jesper In my case, I always get descending order. And I tried all possible case to reverse it. Worst case I will have to turn object into array . – Kishor Pawar May 21 '18 at 13:20
  • 1
    Yes, that does indeed happen *because that is how the language works*. You *will* have to turn object into an array or something else. If it is part of the requirements that the keys of the object are in a particular order, those requirements can't be satisfied. – Jesper May 21 '18 at 13:22
  • it is even more worst, key of an object have an order, it is for each value which could be an index of an array in the ascending order at top of the object. sorting does not affect the objects order. – Nina Scholz May 21 '18 at 14:12