Thomas said:
Also sprach Douglas Crockford:
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