A.sort(null) IE and non-IE

D

ddailey

For reasons that are rather complicated, I have provided my JavaScript
sort() method of an array with the null parameter (rather than an
empty parameter list).

A=[2,3,8,4,1,0]
alert(A.sort(null))

In Opera, FF and Safari, A.sort(null) returns the same array as A, but
IE actually performs the default ascii-order sort. I would assume the
majority of browsers here are following the ECMA-script standard. Is
IE's behavior here a bug?
 
T

Thomas 'PointedEars' Lahn

ddailey said:
A=[2,3,8,4,1,0]
alert(A.sort(null))

In Opera, FF and Safari, A.sort(null) returns the same array as A, but
IE actually performs the default ascii-order sort. I would assume the
majority of browsers here are following the ECMA-script standard. Is
IE's behavior here a bug?

It isn't:

| If comparefn is not undefined and is not a consistent comparison
| function for the elements of this array (see below), the behaviour
| of sort is implementation-defined. [...]

Simply don't pass `null' as argument:

| 15.4.4.11 Array.prototype.sort (comparefn)
|
| The elements of this array are sorted. The sort is not necessarily stable
| (that is, elements that compare equal do not necessarily remain in their
| original order). If comparefn is not undefined, it should be a function
| that accepts two arguments x and y and returns a negative value if x < y,
| zero if x = y, or a positive value if x > y.


PointedEars
 
T

Thomas 'PointedEars' Lahn

Thomas said:
ddailey said:
A=[2,3,8,4,1,0]
alert(A.sort(null))

In Opera, FF and Safari, A.sort(null) returns the same array as A, but
IE actually performs the default ascii-order sort. I would assume the
majority of browsers here are following the ECMA-script standard. Is
_ECMAScript_
IE's behavior here a bug?

It isn't:
[...]

Besides, you have not tested carefully enough.

Test case
----------

var a = [2, 3, 8, 4, 1, 0];
window.alert(a.sort(null));
window.alert(a);

Results
--------

Opera/9.24 (Windows NT 5.1; U; en):

0,1,2,3,4,8
0,1,2,3,4,8

Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.11) Gecko/20071127
Firefox/2.0.0.11:

Error: TypeError: invalid Array.prototype.sort argument
Code: window.alert(a.sort(null));
Line: 2

(no message window)

Mozilla/5.0 (Windows; U; Windows NT 5.1; de-DE) AppleWebKit/523.15 (KHTML,
like Gecko) Version/3.0 Safari/523.15:

Error: TypeError: Null value
Line: 2

(no message window)


PointedEars
 
D

Dr J R Stockton

In comp.lang.javascript message <f735447e-98a7-4235-b710-fa8cb17cec56@m3
4g2000hsf.googlegroups.com>, Thu, 3 Jan 2008 10:27:20, ddailey
For reasons that are rather complicated, I have provided my JavaScript
sort() method of an array with the null parameter (rather than an
empty parameter list).

A=[2,3,8,4,1,0]
alert(A.sort(null))

In Opera, FF and Safari, A.sort(null) returns the same array as A, but
IE actually performs the default ascii-order sort. I would assume the
majority of browsers here are following the ECMA-script standard. Is
IE's behavior here a bug?


The standard is unclear. One cannot tell whether, by 'undefined', it
means 'not-defined' or it means 'the value that U has after var U ;' .
It's also not clear to me whether the standard says what should result
if no argument, or an argument like U, is given.

If you've not done so, it might be worth trying the use of U rather than
null.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top