R
Rene Nyffenegger
Hello everyone.
I am not fluent in JavaScript, so I might overlook the obvious.
But in all other programming languages that I know and that
have associative arrays, or hashes, the elements in the
hash are alphabetically sorted if the key happens to
be alpha numeric. Which I believe makes sense because
it allows for fast lookup of a key.
But in JavaScript, I found this not to be true.
Consider the following function:
function print_assoc_array() {
var assoc_array = new Object();
assoc_array[ "one"] = 1;
assoc_array[ "two"] = 2;
assoc_array["three"] = 3;
assoc_array[ "four"] = 4;
assoc_array[ "five"] = 5;
document.open();
for (i in assoc_array) {
document.writeln(i + " = " + assoc_array + "<br>");
}
document.close();
}
It prints the content of the associative array in insertion
order rather than in alphabetical order (IE6 and FF1.5.0.6).
Am I missing something, or is this expected?
Rene
I am not fluent in JavaScript, so I might overlook the obvious.
But in all other programming languages that I know and that
have associative arrays, or hashes, the elements in the
hash are alphabetically sorted if the key happens to
be alpha numeric. Which I believe makes sense because
it allows for fast lookup of a key.
But in JavaScript, I found this not to be true.
Consider the following function:
function print_assoc_array() {
var assoc_array = new Object();
assoc_array[ "one"] = 1;
assoc_array[ "two"] = 2;
assoc_array["three"] = 3;
assoc_array[ "four"] = 4;
assoc_array[ "five"] = 5;
document.open();
for (i in assoc_array) {
document.writeln(i + " = " + assoc_array + "<br>");
}
document.close();
}
It prints the content of the associative array in insertion
order rather than in alphabetical order (IE6 and FF1.5.0.6).
Am I missing something, or is this expected?
Rene