HighDots Forums  

Does Array.sort() not work the same in all browsers?

Javascript JavaScript language (comp.lang.javascript)


Discuss Does Array.sort() not work the same in all browsers? in the Javascript forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Thomas Mlynarczyk
 
Posts: n/a

Default Does Array.sort() not work the same in all browsers? - 02-22-2004 , 11:56 AM






While experimenting with Array.sort(function) I got different results from
different browsers. How can that be? Shouldn't I expect identical results -
given both the input array and the sort function are identical? What's wrong
here?

Greetings,
Thomas



Reply With Quote
  #2  
Old   
Douglas Crockford
 
Posts: n/a

Default Re: Does Array.sort() not work the same in all browsers? - 02-22-2004 , 12:26 PM






Quote:
While experimenting with Array.sort(function) I got different results from
different browsers. How can that be? Shouldn't I expect identical results -
given both the input array and the sort function are identical? What's wrong
here?
Do you suppose you could share an example with us?


Reply With Quote
  #3  
Old   
Thomas Mlynarczyk
 
Posts: n/a

Default Re: Does Array.sort() not work the same in all browsers? - 02-22-2004 , 03:04 PM



Also sprach Douglas Crockford:

Quote:
Do you suppose you could share an example with us?

Sure:

a = new Array(0,1,2,3,4,5,6,7,8,9);
a = a.sort(function(a,b){return a+3-b;});
t='';
for(i in a) t+= a[i];
alert(t);

IE5 says "1205643987"
Mozilla says "0142857639"
Opera7 says "1052438769"

So why do different browsers give different results with identical code?




Reply With Quote
  #4  
Old   
Douglas Crockford
 
Posts: n/a

Default Re: Does Array.sort() not work the same in all browsers? - 02-22-2004 , 03:23 PM



Quote:
a = new Array(0,1,2,3,4,5,6,7,8,9);
a = a.sort(function(a,b){return a+3-b;});
t='';
for(i in a) t+= a[i];
alert(t);

IE5 says "1205643987"
Mozilla says "0142857639"
Opera7 says "1052438769"

So why do different browsers give different results with identical code?
Your comparison function is at fault, not the sort. The function is
required to act consistently, such that a < b < c.

Since your comparison function can report c < a, you are lying to the
sorter. The differences in the result are due to different implementations.

http://www.crockford.com/


Reply With Quote
  #5  
Old   
Thomas Mlynarczyk
 
Posts: n/a

Default Re: Does Array.sort() not work the same in all browsers? - 02-23-2004 , 05:08 AM



Also sprach Douglas Crockford:

Quote:
Your comparison function is at fault, not the sort. The function is
required to act consistently, such that a < b < c.
But if I want to regard a and b as "equal" for sorting if their values do
not differ by more than 3? I realize now that the outcome depends on the
internal sorting algorithm implemented and that the sorting order is not
completely determined by my function. Still, I feel that sort() ought to
work the same in all browsers. There is no mentioning of this problem in any
of the documentation I have. On your website, you recommend some good books
on JavaScript - aren't there some good links as well?




Reply With Quote
  #6  
Old   
Fox
 
Posts: n/a

Default Re: Does Array.sort() not work the same in all browsers? - 02-23-2004 , 06:56 AM





Thomas Mlynarczyk wrote:
Quote:
Also sprach Douglas Crockford:

Your comparison function is at fault, not the sort. The function is
required to act consistently, such that a < b < c.

But if I want to regard a and b as "equal" for sorting if their values do
not differ by more than 3? I realize now that the outcome depends on the
internal sorting algorithm implemented and that the sorting order is not
completely determined by my function. Still, I feel that sort() ought to
work the same in all browsers. There is no mentioning of this problem in any
of the documentation I have. On your website, you recommend some good books
on JavaScript - aren't there some good links as well?
you're thinking mathematically -- sort functions are logical
(comparative)... [I wouldn't be surprised if some of the sort algorithms
implemented in different browsers attach significance to the magnitude
of the result -- I would -- if I were playing around]

documentation usually refers to returning the values -1, 0, or 1 and
your function is returning the result of a mathmatical expression.

try function cmp(a,b) { return a + 3 - b < 0 ? -1 : 1; }
//(0 and 1 have the same result -- 0 means a = b, order unchanged --
whereas 1 means b > a, the order also remains the same

and see if that gets you consistent results (i don't have time to test
it myself).


Fox


Reply With Quote
  #7  
Old   
Douglas Crockford
 
Posts: n/a

Default Re: Does Array.sort() not work the same in all browsers? - 02-23-2004 , 08:38 AM



Quote:
Your comparison function is at fault, not the sort. The function is
required to act consistently, such that a < b < c.

But if I want to regard a and b as "equal" for sorting if their values do
not differ by more than 3? I realize now that the outcome depends on the
internal sorting algorithm implemented and that the sorting order is not
completely determined by my function. Still, I feel that sort() ought to
work the same in all browsers. There is no mentioning of this problem in any
of the documentation I have. On your website, you recommend some good books
on JavaScript - aren't there some good links as well?
Try ordering them yourself by hand with your comparison function. Then
you may get a sense of how your function is at fault. It is illogical to
expect compliant behavior when your own behavior is not compliant.

The Standard does not call for a particular sorting algorithm. Different
algorithms will examine the pairs of keys in different sequences. When
the comparison is correct (a < b, b < c, a < c) then they call have the
same result. When the comparison is wrong (a < b, b < c, a > c) then
you cannot expect the result to be stable.

Check out
http://www.ecma-international.org/pu...s/ECMA-262.HTM
for the specification of the sort method.


Reply With Quote
  #8  
Old   
Lasse Reichstein Nielsen
 
Posts: n/a

Default Re: Does Array.sort() not work the same in all browsers? - 02-23-2004 , 11:38 AM



"Thomas Mlynarczyk" <blue_elephant55 (AT) hotmail (DOT) com> writes:

Quote:
But if I want to regard a and b as "equal" for sorting if their values do
not differ by more than 3?
Then you should consistently return 0 when a and b are compared, no matter
in what order.

Quote:
I realize now that the outcome depends on the internal sorting
algorithm implemented and that the sorting order is not completely
determined by my function. Still, I feel that sort() ought to work
the same in all browsers.
That is not required by the standard. Only the behavior for consistent
comparison functions (representing a transitive, anti-reflexive
relation[1]) is defined. In that case, the result is sorted.

Quote:
There is no mentioning of this problem in any of the documentation I
have. On your website, you recommend some good books on JavaScript -
aren't there some good links as well?
The authoritative definition of ECMAScript (the standardization of
Javascript) is here:
<URL:http://www.mozilla.org/js/language/E262-3.pdf>


As a side note, both Opera and Mozilla implements the ECMAScript sort
incorrectly - they fail (almost unnoticeably) when there are empty
slots in the array
This expression should give false:
2 in ([3,,1].sort())
(IE has other errors regarding arrays, so no browser is perfect

/L

[1] I.e., for all a,b,c:
a<b & b<c => a<c (transitive)
and
a<b & b<a => b=a (antireflexive)
--
Lasse Reichstein Nielsen - lrn (AT) hotpop (DOT) com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'


Reply With Quote
  #9  
Old   
Steve van Dongen
 
Posts: n/a

Default Re: Does Array.sort() not work the same in all browsers? - 02-23-2004 , 12:08 PM



"Thomas Mlynarczyk" <blue_elephant55 (AT) hotmail (DOT) com> wrote:

Quote:
Also sprach Douglas Crockford:

Your comparison function is at fault, not the sort. The function is
required to act consistently, such that a < b < c.

But if I want to regard a and b as "equal" for sorting if their values do
not differ by more than 3? I realize now that the outcome depends on the
internal sorting algorithm implemented and that the sorting order is not
completely determined by my function. Still, I feel that sort() ought to
work the same in all browsers. There is no mentioning of this problem in any
of the documentation I have. On your website, you recommend some good books
on JavaScript - aren't there some good links as well?
If you want to treat them as equal then you have to write that into
your comparison function. Still, this may work differently in various
browsers but at least it will do what you want.

function(a, b)
{
if (Math.abs(a-b)) <= 3)
return 0;
else
return a-b;
}


Reply With Quote
  #10  
Old   
Thomas Mlynarczyk
 
Posts: n/a

Default Re: Does Array.sort() not work the same in all browsers? - 02-24-2004 , 04:22 AM



Also sprach Steve van Dongen:

Quote:
If you want to treat them as equal then you have to write that into
your comparison function. Still, this may work differently in various
browsers but at least it will do what you want.

function(a, b)
{
if (Math.abs(a-b)) <= 3)
return 0;
else
return a-b;
}
Thanks, but I think I have tried that version too. Without success. The
point is, as I have now realized, that - no matter how the function is
defined - the outcome will always depend on the way sort() is implemented.
If I regard two numerically different values as equal for the sorting, but
then still regard them as different in the result, the whole idea cannot
work. Thanks to you all for having helped me understand that.





Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.