P
P
Pavlo Ponomarenko2011-01-10 23:15:23
JavaScript
Pavlo Ponomarenko, 2011-01-10 23:15:23

Chrome sort properties in hash

Hi guys. A question. Chrome has a problem - it sorts properties in a hash. For example,

var hash = {
  1 : 'один',
  3: 'три',
  2: 'два'
};
for (var i in hash) alert(hash[i]); 

Will print "one, two, three", although a completely different order is expected.

Another example is [].associatefrom MooTools:
Array.prototype.associate = function(keys){
    var obj = {}, length = Math.min(this.length, keys.length);
    for (var i = 0; i < length; i++) obj[keys[i]] = this[i];
    return obj;
};

var hash = ['one', 'three', 'two'].associate(['first','3','2']);

var string = '';

for (var i in hash) string += i + ':' + hash[i] + '; ';

alert(string);
// Chrome: 2:two; 3:three; first:one;
// Other : first:one; 3:three; 2:two;



I consider this behavior completely illogical and, at least, not cross-browser, and therefore I would like to find a way to force Chrome not to engage in amateur activity and sort the properties in the order that I set, and not alphabetically.
Any ideas how to do it?

Answer the question

In order to leave comments, you need to log in

8 answer(s)
A
Alexey Grichenko, 2011-01-10
@Kalobok

I don't know about browsers, but usually hashes don't guarantee any particular order of elements, either original or sorted. It may turn out to be too expensive to do both a quick search by key (which, in fact, the hash is intended for), and store the original order of the elements.

M
my_own_parasite, 2011-01-11
@my_own_parasite

from ECMA-262:
12.6.4 The for-in Statement

The mechanics and order of enumerating the properties (step 6.a in the first algorithm, step 7.a in the second) is not specified.
...
amateur performance is spelled out in the standard. therefore, it is impossible to consider the random for in behavior you need in other browsers as "standard" :)

M
Mikhail Davydov, 2011-01-11
@azproduction

TheShock, this is a feature / bug of chrome (MB WebKit) in other browsers everything is as it was before. Humble it has been around for a long time.

T
TimTowdy, 2011-01-11
@TimTowdy

Will print "one, two, three", although a completely different order is expected.
What does expected mean? Order in hashes cannot be expected. Even if the same hash outputs 1,2,3 today and 2,1,3 tomorrow, this is quite normal behavior.
I find this behavior completely illogical and at least not cross-browser friendly.
And I think that bubble sort should have O(1) complexity. How can chrome developers be forced to change the laws of nature?
You have the amazing audacity to demand some behavior from a hash without understanding how it works.
We need a guaranteed selection order from the hash - take all its keys, sort it, and access it one by one. You need a selection in the order of insertion - store the array with the keys separately.

P
Puma Thailand, 2011-01-11
@opium

In my opinion, you can hit the wall if you rely on the order of the keys in the hash.

K
Konstantin Kitmanov, 2011-01-11
@k12th

Opera doesn't sort either (at least some versions). If this is the case in IE and FF, these are just features of these implementations. It seems that even the standard states that the extraction order is not guaranteed.
And in general, dictionaries have never been sorted anywhere (except maybe PHP).

M
mraleph, 2011-01-11
@mraleph

This is not alphabetical order. First all numeric properties in numerical order, then all non-numeric properties in insertion order.
Check out the V8 issue tracker (Working As Intended resolution): code.google.com/p/v8/issues/detail?id=164
The new JavaScript standard will most likely specify exactly this behavior: wiki.ecmascript.org/doku.php?id =strawman :enumeration If
you want to keep the order of the keys, keep the keys side by side in an array in the order you want and use when iterating.

M
mishutkiss, 2011-01-11
@mishutkiss

in the first example you specify a rigid sequence: the first property is ONE, the second is TWO, the third is THREE only in a different order and it is logical to expect just such a result
var hash = {
1: 'one',
3: 'three',
2: 'two'
};
is equivalent to
var hash = {
1: 'one',
2: 'two',
3: 'three'
};

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question