If

comparefnis not`undefined`

and is not a consistent comparison function for the elements of this array (see below), the behaviour of`sort`

is implementation-defined.[…]

A function

comparefnis a consistent comparison function for a set of valuesSif all of the requirements below are met for all valuesa,b, andc(possibly the same value) in the setS: The notationa<_{CF}bmeanscomparefn(a,b) < 0;a=_{CF}bmeanscomparefn(a,b) = 0 (of either sign); and a >_{CF}bmeanscomparefn(a,b) > 0.

- Calling
comparefn(a,b) always returns the same valuevwhen given a specific pair of valuesaandbas its two arguments. Furthermore, Type(v) is Number, andvis not`NaN`

. Note that this implies that exactly one ofa<_{CF}b,a=_{CF}b, anda>_{CF}bwill be true for a given pair ofaandb.- Calling
comparefn(a,b) does not modify the`this`

object.a=_{CF}a(reflexivity)- If
a=_{CF}b, thenb=_{CF}a(symmetry)- If
a=_{CF}bandb=_{CF}c, thena=_{CF}c (transitivity of =_{CF})- If
a<_{CF}bandb<_{CF}c, thena<_{CF}c(transitivity of <_{CF})- If
a>_{CF}bandb>_{CF}c, thena>_{CF}c(transitivity of >_{CF})

ECMA-262 3rd edition or ECMA-262 5th edition, 15.4.4.11 Array.prototype.sort (comparefn)

Could you explain the significance of this quote? What was the impetus of this post? I’m not picking up on it. Is it the inclusion of typical axioms for mathematical (in)equality?

Comment by Colby Russell — 01.03.10 @ 09:29

Here’s the impetus.

Comment by Jeff — 01.03.10 @ 11:38

If you do not want to follow the link, the link points you to the discussion on the non-randomness of the browser ballot due to the undefined behavior.

Comment by Havvy — 01.03.10 @ 16:38

Just curious, why would someone interested in knowing the impetus not be willing to follow the link?

Comment by Jeff — 01.03.10 @ 19:02