Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Compare function recieves arguments <code>a</code> and <code>b</code>.</p> <p>It must return </p> <ul> <li>-1 if <code>a</code> has to be placed before <code>b</code></li> <li>0 if positions of <code>b</code> and <code>a</code> are equal</li> <li>1 if <code>a</code> has to be placed after <code>b</code></li> </ul> <p>You are returning whatever values, not just those three listed, which is why order gets messed up.</p> <p>Compare the results in example below.</p> <pre><code>var test:Vector.&lt;Number&gt;; // returning whatever the difference is var sortMethod1:Function = function (a:Number, b:Number) : Number { var value:Number = a-b; trace(a+"\t"+b +"\t= "+ value); return value; } // returning -1, 0, 1 var sortMethod2:Function = function (a:Number, b:Number) : int { var result:int; if (a &lt; b) { result = -1; } else if (a &gt; b) { result = 1; } else { result 0; } trace(a+"\t"+b +"\t: "+ result); return result; } test = new &lt;Number&gt;[1.2,1.1,1.4,1.5,0]; trace("BEFORE", test); test.sort(sortMethod1); trace("AFTER1", test); trace("--"); test = new &lt;Number&gt;[1.2,1.1,1.4,1.5,0]; trace("BEFORE", test); test.sort(sortMethod2); trace("AFTER2", test); /* Trace output: BEFORE 1.2,1.1,1.4,1.5,0 1.1 1.4 = -0.2999999999999998 1.2 1.4 = -0.19999999999999996 1.5 1.4 = 0.10000000000000009 0 1.4 = -1.4 0 1.4 = -1.4 1.1 1.2 = -0.09999999999999987 0 1.2 = -1.2 1.5 1.2 = 0.30000000000000004 1.5 1.2 = 0.30000000000000004 0 1.2 = -1.2 0 1.1 = -1.1 AFTER1 0,1.1,1.2,1.5,1.4 -- BEFORE 1.2,1.1,1.4,1.5,0 1.1 1.4 : -1 1.2 1.4 : -1 1.5 1.4 : 1 0 1.4 : -1 1.5 1.4 : 1 0 1.4 : -1 0 1.1 : -1 1.1 1.2 : -1 AFTER2 0,1.1,1.2,1.4,1.5 */ </code></pre> <p><strong>EDIT</strong> In your case that would be the following:</p> <pre><code>private function cmpr(h1:HelpObj, h2:HelpObj):int { var hx1:Number = h1.value.x; var hx2:Number = h2.value.x; if (hx1 &lt; hx2) { return -1; } if (hx1 &gt; hx2) { return 1; } var hy1:Number = h1.value.y; var hy2:Number = h2.value.y; if (hy1 &lt; hy2) { return -1; } if (hy1 &gt; hy2) { return 1; } return 0; } </code></pre>
 

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