Chrome sorts the properties in the hash

0 like 0 dislike
62 views
Hi guys. A question. In Chrome there is a problem — it sorts the properties in the hash. For example,
var hash = { 1 : 'one', 3: 'three', 2: 'two' }; for (var i in hash) alert(hash[i]); 

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

Another example is [].associate of 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 believe this behavior is completely illogical and, at least, is not cross-browser because I would like to find a way to force Chrome not to be engaged in Amateur performance and to sort the properties in the order that I ask, not alphabetical.
Any idea how to do it?
by | 62 views

7 Answers

0 like 0 dislike
I don't know about browsers, but usually the hashes don't guarantee any specific order of the elements is neither the source nor sorted. Too expensive it may be to do a quick search on the key (which, in fact, is a hash), and store the original order of the elements.
by
0 like 0 dislike
from ECMA-262:
\r
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.
...
\r
the initiative spelled out in the standard. so take a random desired behavior for in other browsers "standard" do not :)
by
0 like 0 dislike
TheShock, it's a feature/bug of chrome(WebKit MB) in other browsers everything is as it was before. Face it he is a long time.
by
0 like 0 dislike
Will output "one, two, three", although it is expected a completely different order.
What do you mean expected? Order in hashes cannot be expected. Even if the same hash will output 1,2,3 today and tomorrow 2,1,3 — this is quite normal behavior.
\r
I believe this behavior is completely illogical and, at least, is not cross-browser
And I think that bubble sort should be O(1) complexity. As the developers of chrome to make the change to the laws of nature?
You have the amazing audacity to demand some behavior from a hash, not understanding how it works.
\r
Need a guaranteed order of sampling from hash take his keys, sort, and speak in turn. Need a sample insertion order — keep a separate array of keys.
by
0 like 0 dislike
I think you can hit a wall if you rely on order of keys in the hash.
by
0 like 0 dislike
Opera also does not sort (at least, some versions). If this is the case in IE and FF — it's just a particular data implementation. Like even in standard it is specified that the order of retrieval is not guaranteed.
And indeed dictionaries had never been sorted anywhere else (except maybe PHP).
by
0 like 0 dislike
It's not alphabetical order. First, all the numeric properties in numerical order, then all non-numeric in the order of insertion.
\r
Yishui in our bugtracker V8 (resolution Working As Intended): code.google.com/p/v8/issues/detail?id=164
\r
The new JavaScript standard is likely to specify this behavior: wiki.ecmascript.org/doku.php?id=strawman:enumeration
\r
Want to keep the order of the keys, keep the keys next to the array in the order you want and use when iterating.
by
110,608 questions
257,186 answers
0 comments
28,881 users