breaking down how to sort numbers in an array
<p>I'm going over <a href="http://www.javascriptkit.com/javatutors/arraysort.shtml" rel="nofollow">this</a> sorting tutorial and I'm  having problems understanding it.
I see that the rules are the following when passing in a sortfunction to <code>.sort()</code>.</p>

<blockquote>
  <ul>
  <li>Less than 0: Sort "a" to be a lower index than "b"</li>
  <li>Zero: "a" and "b" should be considered equal, and no sorting    performed.</li>
  <li>Greater than 0: Sort "b" to be a lower index than "a".</li>
  </ul>
</blockquote>

<p>and I'm testing this snippet:</p>

<pre><code>var myarray=[25, 8, 7, 41];
console.log(myarray);
console.log(myarray.sort(function(a,b) {
    console.log("a : " , a , " b : " , b, " oper : " , b-a, " arr ");
    return b -a
}));
</code></pre>

<p>the main part is <code>return b -a</code> and this should return the array in descending order I  think because if <code>b</code> is larger than <code>a</code> it will be placed in a lower index. I think I'm having a hard time under standing what exactly "lower" means. does it mean that it b goes directly before <code>a</code> or does <code>b</code> go to the beginning of the array? </p>

<p>here is where I'm getting confused. The result of the code is:</p>

<pre><code>[25, 8, 7, 41]
a : 25  b : 8  oper : -17  arr
a : 8  b : 7  oper : -1  arr 
a : 7  b : 41  oper : 34  arr 
a : 25  b : 41  oper : 16  arr 
[41, 25, 8, 7]
</code></pre>

<p>here is just my thinking </p>

<p>since the first 2 comparisons produce a negative <code>a</code> goes before <code>b</code> so at the end of the first 2 comparisons the array should be something like <code>[25,8,7,41]</code> (no change). </p>

<p>Then the next comparison is a positive number(34) so  so I think <code>b</code> goes before <code>a</code> so it will be now <code>[25,8,41,7]</code> then I don't why it compares 25 and 41 next How is <code>a</code> = 25 and <code>b</code> = 41? can you also show me how to view the array as it is being transformed I tried to do that without success. Thanks in advance.</p>

<p>If you see another question that explains this sorting breakdown in detail I'll be happy to close this question. I don't mean to ask useless questions on SO. most tuts just explain how to sort in general but not break it down. Thank you</p>