Underscore: groupBy ignores/destroys sort order?

Created on 24 Jul 2011  路  1Comment  路  Source: jashkenas/underscore

I have a sorted array, and upon passing it through _.groupBy, it seems as though it returns results out-of-order from what I passed values in.

Example (sorted in descending order by each column):

[ { key: [ 2887902, 2011, 7, 13, 13, 14 ],
value: [ 0.27999999999883585, 0.5, 1 ] },
{ key: [ 2887902, 2011, 7, 13, 7, 15 ],
value: [ 5.149999999994179, 2.5, 1 ] },
{ key: [ 2887902, 2011, 7, 13, 6, 26 ],
value: [ 0, 0.5, 1 ] },
{ key: [ 2820795, 2012, 7, 23, 19, 14 ],
value: [ 17.349999999976717, 6, 1 ] },
{ key: [ 2820795, 2011, 7, 15, 13, 3 ],
value: [ 3.4349999999976717, 3.5, 1 ] },
{ key: [ 2820795, 2011, 7, 15, 12, 55 ],
value: [ 188.02000000001863, 118.5, 1 ] },
{ key: [ 2820795, 2011, 7, 14, 18, 11 ],
value: [ 17.51500000001397, 7.5, 1 ] },
{ key: [ 2820795, 2011, 7, 14, 17, 43 ],
value: [ 9.46999999997206, 5, 1 ] },
{ key: [ 2820795, 2011, 6, 23, 19, 14 ],
value: [ 17.320000000006985, 7, 1 ] } ]

var output = _.groupBy(array, function(out_key) {
return out_key.key[0];
}

My output is in the following order:

[ { key: [ 2820795, 2012, 7, 23, 19, 14 ],
value: [ 17.349999999976717, 6, 1 ] },
{ key: [ 2820795, 2011, 7, 15, 13, 3 ],
value: [ 3.4349999999976717, 3.5, 1 ] },
{ key: [ 2820795, 2011, 7, 15, 12, 55 ],
value: [ 188.02000000001863, 118.5, 1 ] },
{ key: [ 2820795, 2011, 7, 14, 18, 11 ],
value: [ 17.51500000001397, 7.5, 1 ] },
{ key: [ 2820795, 2011, 7, 14, 17, 43 ],
value: [ 9.46999999997206, 5, 1 ] },
{ key: [ 2820795, 2011, 6, 23, 19, 14 ],
value: [ 17.320000000006985, 7, 1 ] } ]

[ { key: [ 2887902, 2011, 7, 13, 13, 14 ],
value: [ 0.27999999999883585, 0.5, 1 ] },
{ key: [ 2887902, 2011, 7, 13, 7, 15 ],
value: [ 5.149999999994179, 2.5, 1 ] },
{ key: [ 2887902, 2011, 7, 13, 6, 26 ],
value: [ 0, 0.5, 1 ] } ]

Is this expected or have I done something wrong? (Within the two result sets, the results are sorted. But I would have expected to see 2887902 first, then 2820795.)

question

Most helpful comment

The underlying issue here is that most engines preserve insertion order except for array indices. See, for example, http://code.google.com/p/v8/issues/detail?id=164

So, in Chrome:

t = {}; t['two'] = 2; t['one'] = 1;
_.keys(t)
> ["two", "one"] // insertion order preserved

t = {}; t[2] = 2; t[1] = 1;
_.keys(t)
> [1, 2] // insertion order not preserved

Since your groupBy function returns integers, they are treated as array indices and insertion order is lost.

>All comments

The underlying issue here is that most engines preserve insertion order except for array indices. See, for example, http://code.google.com/p/v8/issues/detail?id=164

So, in Chrome:

t = {}; t['two'] = 2; t['one'] = 1;
_.keys(t)
> ["two", "one"] // insertion order preserved

t = {}; t[2] = 2; t[1] = 1;
_.keys(t)
> [1, 2] // insertion order not preserved

Since your groupBy function returns integers, they are treated as array indices and insertion order is lost.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

clouddueling picture clouddueling  路  3Comments

ksullivan picture ksullivan  路  9Comments

githublyp picture githublyp  路  3Comments

arypbatista picture arypbatista  路  3Comments

haggholm picture haggholm  路  8Comments