1

In IE 9, if I type this in the console:

[1, 4, 2, 3].sort(function (a, b) { return a < b; });

The resulting array is: [1, 4, 2, 3].

If I do this in FF/Chrome, I get it, reverse-sorted: [4, 3, 2, 1].

How come this doesn't work in IE?

EDIT: Is there a function out there that abstracts these browser differences? In other words, is there a function I can pass function(a, b) { return a < b; } in and get the same result in all browsers? Any open-source stuff?

2
  • Possible duplicate of stackoverflow.com/questions/4782893/… Commented Sep 21, 2011 at 14:29
  • I see this answer you have linked but I don't understand how to make it work in IE like it does in FF/Chrome. Commented Sep 21, 2011 at 14:31

1 Answer 1

7

Maybe because the function is supposed to return -1 if a is smaller, 0 if they are equal, and 1 if a is larger (or vice versa if you want reverse order). Edit: In fact, it must be zero, a positive or negative number (as @pimvdb pointed out, and that's what I make use of in the example below). Your function will never return -1 though and that might create problems.

Consider 1 and 3. Your function returns 1 for 1 < 3, which is ok, but returns 0 for 3 < 1. In one case the number are different, in the other you are saying that they are equal.

That FF/Chrome sort it in reverse order might be due to the sorting algorithm they are using.

Try:

[1, 4, 2, 3].sort(function (a, b) { return b - a; });

Update: To substantiate this, we can have a look at the specification, Section 15.4.4.11 Array.prototype.sort(comparefn), where the properties are given which have to be fulfilled by a comparison function:

A function comparefn is a consistent comparison function for a set of values S if all of the requirements below are met for all values a, b, and c (possibly the same value) in the set S: The notation a <CF b means comparefn(a,b) < 0; a =CF b means comparefn(a,b) = 0 (of either sign); and a >CF b means comparefn(a,b) > 0.

  • Calling comparefn(a,b) always returns the same value v when given a specific pair of values a and b as its two arguments. Furthermore, Type(v) is Number, and v is not NaN. Note that this implies that exactly one of a <CF b, a =CF b, and a >CF b will be true for a given pair of a and b.
  • Calling comparefn(a,b) does not modify the this object.
  • a =CF a (reflexivity)
  • If a =CF b, then b =CF a (symmetry)
  • If a =CF b and b =CF c, then a =CF c (transitivity of =CF)
  • If a <CF b and b <CF c, then a <CF c (transitivity of <CF)
  • If a >CF b and b >CF c, then a >CF c (transitivity of >CF)

NOTE The above conditions are necessary and sufficient to ensure that comparefn divides the set S into equivalence classes and that these equivalence classes are totally ordered.

Sign up to request clarification or add additional context in comments.

3 Comments

+1 Although it's rather a negative/positive/zero number. Yours doesn't always return -1, 1 or 0 either :)
ES5 #15.4.4.11 - sais you must return 1, 0 or -1 in custom sort function. ecma-international.org/publications/files/ECMA-ST/Ecma-262.pdf
@c69: As far as I understood it, this is what the internal compare function returns. A function passed as parameter just has to follow these rules: The notation a <CF b means comparefn(a,b) < 0; a =CF b means comparefn(a,b) = 0 (of either sign); and a >CF b means comparefn(a,b) > 0. But thanks for the reference!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.