![]() | |
![]() |
| | Thread Tools | Display Modes |
#1
| |||
| |||
|
#2
| |||
| |||
|
|
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? |
#3
| |||
| |||
|
|
Do you suppose you could share an example with us? |
#4
| |||
| |||
|
|
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? |
#5
| |||
| |||
|
|
Your comparison function is at fault, not the sort. The function is required to act consistently, such that a < b < c. |
#6
| |||
| |||
|
|
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 I were playing around]
#7
| |||
| |||
|
|
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? |
#8
| |||
| |||
|
|
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? |


#9
| |||
| |||
|
|
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? |
#10
| |||
| |||
|
|
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; } |
![]() |
| Thread Tools | |
| Display Modes | |
| |