3

I have an array of simple objects that looks like the following:

var objects =[
    {
      "group": "05",
      "format": "Legacy",
      "active": false
    },
    {
      "group": "05",
      "format": "Legacy",
      "active": false
    },
    {
      "group": "23",
      "format": "Legacy",
      "active": true
    }
]

I would like to use underscore's groupBy function to group these items, while maintaining the order of the attribute being grouped by.

When calling _.groupBy(objects, 'group'), the result looks like this:

{
  "23": [
    {
      "group": "23",
      "format": "Legacy",
      "active": true
    }
  ],
  "05": [
    {
      "group": "05",
      "format": "Legacy",
      "active": false
    },
    {
      "group": "05",
      "format": "Legacy",
      "active": false
    }
  ]
}

Is there a particular reason the '23' grouping gets placed in the resultant object before '05'? It doesn't seem to respect either natural ordering or the order of the array being grouped on.

Ben Siver
  • 2,758
  • 1
  • 25
  • 42

4 Answers4

3

You can consider the result of _.groupBy a hashtable, since Objects in JS are usually implemented as unordered hashtables for constant time retrieval of property values etc.

Javascript hashtable implementations should not guarantee the sorting order of the keys, so you can't count on sorting when using them.

Per the comments by @mu is too short, that doesn't mean that in the future some ES* implementation won't have ordered hashtables.

Faris Zacina
  • 14,056
  • 7
  • 62
  • 75
  • 2
    Some key/value data structures are hash tables and are ordered (Ruby's `Hash` for example), most aren't ordered. The JavaScript spec says nothing about how `Object`s are implemented and doesn't say that `Object`s are ordered (references [over here](http://stackoverflow.com/a/22845062/479863)). `_.groupBy` returns an `Object` so `_.groupBy`'s return value is not guaranteed to be ordered. – mu is too short Dec 04 '14 at 18:02
  • Yeah ur right.. there are some exceptions, e.g. Wikipedia classifies Hashtables in the "Unordered associative array" type, but i agree that it is best to verify the specific implementation. The Ruby Hash implementation preserves the insertion order, which is an exception and not the rule for common hashtables across different programming languages. I will correct my answer to be more accurate. Thanks! – Faris Zacina Dec 04 '14 at 18:21
2

You're missing the main point: JavaScript objects are not ordered data sets.

However, particular items inside each group will preserve their ordering. Just the groups will be messed up because, as said before, they are keys of object and cannot be ordered.

Andrew Dunai
  • 3,061
  • 19
  • 27
2

It returns an object, which loses its original order. If you need an alternative that keeps the order and returns an array (instead of an object), I've created a gist that you can use https://gist.github.com/superelement/69677823acf20d45fdbb11a051486936

This question is similar to this thread lodash sortBy then groupBy, is order maintained?

Community
  • 1
  • 1
Jim Doyle
  • 2,302
  • 1
  • 16
  • 12
0

The problem with _.groupBy from Underscore.js is when we perform on numeric property of collection it does apply ascending sorting automatically, and for string property it does preserves the sequence of collection.

So if someone is using _.groupBy on numeric property then need to apply sorting again on grouped collection if want to apply specific sort order on collection.

Maulik
  • 510
  • 1
  • 7
  • 22