H
Hamish Campbell
Hi all,
Does anyone understand how the 'default' sort algorithms for the major
browsers work? I was playing with .sort() after reading a previous
thread about array sorting. I was curious, because the ECMAScript
standard seems a bit silent on the issue, and array sorting seems to
be (more or less) implementation dependent. I could also be
misunderstanding how an array is enumerated with for-in - is the
GetValue property of an array specific to the implementation too?
Example:
<script language="javascript" type="text/javascript">
var data = new Array;
data[2] = 'integer (2)';
data[3] = 'integer (3)';
data[1] = 'integer (1)';
console.log("Before Sort");
for(var index in data)
console.log("index " + index + " was of type " + data[index]);
data.sort();
console.log("After Sort");
for(var index in data)
console.log("index " + index + " was of type " + data[index]);
</script>
FF3 and Chrome produce the output:
Before Sort
index 1 was of type integer (1)
index 2 was of type integer (2)
index 3 was of type integer (3)
After Sort
index 0 was of type integer (1)
index 1 was of type integer (2)
index 2 was of type integer (3)
IE8 produces the same result, but in the reverse order.
This is expected. However, if I add a string based key, such as below:
<script language="javascript" type="text/javascript">
var data = new Array;
data[2] = 'integer (2)';
data[3] = 'integer (3)';
data[1] = 'integer (1)';
data['a'] = 'string (a)';
</script>
I get the following output (FF3 3.0.10):
Before Sort
index 1 was of type integer (1)
index 2 was of type integer (2)
index 3 was of type integer (3)
index a was of type string (a)
After Sort
index 1 was of type integer (2)
index 2 was of type integer (3)
index a was of type string (a)
index 0 was of type integer (1)
Chrome (2.0.172.31) returns:
Before Sort
index a was of type string (a)
index 1 was of type integer (1)
index 2 was of type integer (2)
index 3 was of type integer (3)
After Sort
index a was of type string (a)
index 1 was of type integer (2)
index 2 was of type integer (3)
index 0 was of type integer (1)
IE8 returns:
Before Sort
index 2 was of type integer (2)
index 3 was of type integer (3)
index 1 was of type integer (1)
index a was of type string (a)
After Sort
index 2 was of type integer (3)
index 1 was of type integer (2)
index a was of type string (a)
index 0 was of type integer (1)
Thanks,
Hamish
Does anyone understand how the 'default' sort algorithms for the major
browsers work? I was playing with .sort() after reading a previous
thread about array sorting. I was curious, because the ECMAScript
standard seems a bit silent on the issue, and array sorting seems to
be (more or less) implementation dependent. I could also be
misunderstanding how an array is enumerated with for-in - is the
GetValue property of an array specific to the implementation too?
Example:
<script language="javascript" type="text/javascript">
var data = new Array;
data[2] = 'integer (2)';
data[3] = 'integer (3)';
data[1] = 'integer (1)';
console.log("Before Sort");
for(var index in data)
console.log("index " + index + " was of type " + data[index]);
data.sort();
console.log("After Sort");
for(var index in data)
console.log("index " + index + " was of type " + data[index]);
</script>
FF3 and Chrome produce the output:
Before Sort
index 1 was of type integer (1)
index 2 was of type integer (2)
index 3 was of type integer (3)
After Sort
index 0 was of type integer (1)
index 1 was of type integer (2)
index 2 was of type integer (3)
IE8 produces the same result, but in the reverse order.
This is expected. However, if I add a string based key, such as below:
<script language="javascript" type="text/javascript">
var data = new Array;
data[2] = 'integer (2)';
data[3] = 'integer (3)';
data[1] = 'integer (1)';
data['a'] = 'string (a)';
</script>
I get the following output (FF3 3.0.10):
Before Sort
index 1 was of type integer (1)
index 2 was of type integer (2)
index 3 was of type integer (3)
index a was of type string (a)
After Sort
index 1 was of type integer (2)
index 2 was of type integer (3)
index a was of type string (a)
index 0 was of type integer (1)
Chrome (2.0.172.31) returns:
Before Sort
index a was of type string (a)
index 1 was of type integer (1)
index 2 was of type integer (2)
index 3 was of type integer (3)
After Sort
index a was of type string (a)
index 1 was of type integer (2)
index 2 was of type integer (3)
index 0 was of type integer (1)
IE8 returns:
Before Sort
index 2 was of type integer (2)
index 3 was of type integer (3)
index 1 was of type integer (1)
index a was of type string (a)
After Sort
index 2 was of type integer (3)
index 1 was of type integer (2)
index a was of type string (a)
index 0 was of type integer (1)
Thanks,
Hamish