Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<blockquote> <p>See also <a href="https://stackoverflow.com/q/20883421/1048572">Sorting in JavaScript: Should every compare function have a &quot;return 0&quot; statement?</a></p> </blockquote> <hr> <pre><code>if (a.to != b.from) return 1; if (a.to == b.from) return -1; </code></pre> <p>That's not a consistent compare function (violating the reflexivity, i.e. <code>compare(x, x) == 0</code>, for example). What do you expect it to do?</p> <p>Citing the <a href="http://es5.github.com/#x15.4.4.11" rel="nofollow noreferrer">ES5.1 spec on <code>sort</code></a>:</p> <blockquote> <p>If <code>comparefn</code> is […] not a consistent comparison function for the elements of this array, the behaviour of sort is implementation-defined.</p> <p>A function <code>comparefn</code> is a consistent comparison function for a set of values <code>S</code> if all of the requirements below are met for all values <code>a</code>, <code>b</code>, and <code>c</code> (possibly the same value) in the set <code>S</code>: The notation <code>a &lt;CF b</code> means <code>comparefn(a,b) &lt; 0</code>; <code>a =CF b</code> means <code>comparefn(a,b) = 0</code> (of either sign); and <code>a &gt;CF b</code> means <code>comparefn(a,b) &gt; 0</code>.</p> <p>Calling <code>comparefn(a,b)</code> always returns the same value <code>v</code> when given a specific pair of values <code>a</code> and <code>b</code> as its two arguments. Furthermore, <code>Type(v)</code> is Number, and <code>v</code> is not <code>NaN</code>. Note that this implies that exactly one of <code>a &lt;CF b</code>, <code>a =CF b</code>, and <code>a &gt;CF b</code> will be true for a given pair of <code>a</code> and <code>b</code>.</p> <ul> <li>Calling <code>comparefn(a,b)</code> does not modify the this object.</li> <li><code>a =CF a</code> (reflexivity)</li> <li>If <code>a =CF b</code>, then <code>b =CF a</code> (symmetry)</li> <li>If <code>a =CF b</code> and <code>b =CF c</code>, then <code>a =CF c</code> (transitivity of <code>=CF</code>)</li> <li>If <code>a &lt;CF b</code> and <code>b &lt;CF c</code>, then <code>a &lt;CF c</code> (transitivity of <code>&lt;CF</code>)</li> <li>If <code>a &gt;CF b</code> and <code>b &gt;CF c</code>, then <code>a &gt;CF c</code> (transitivity of <code>&gt;CF</code>)</li> </ul> <p>NOTE: The above conditions are necessary and sufficient to ensure that <code>comparefn</code> divides the set <code>S</code> into equivalence classes and that these equivalence classes are totally ordered.</p> </blockquote>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload