Answer the question
In order to leave comments, you need to log in
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]);
[].associate
from 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;
Answer the question
In order to leave comments, you need to log in
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.
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" :)
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.
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?
In my opinion, you can hit the wall if you rely on the order of the keys in the hash.
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).
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.
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 questionAsk a Question
731 491 924 answers to any question