title: Algorithm of JavaScript "sort()" Function
body: <p>Recently when I was working with JavaScript "sort()" function, I found in one of the <a href="http://www.w3schools.com/jsref/jsref_sort.asp" rel="noreferrer">tutorials</a> that this function does not sort the numbers properly. Instead to sort numbers, a function must be added that compares numbers, like the following code:-</p>
  
  <pre><code>&lt;script type="text/javascript"&gt;
  function sortNumber(a,b)
  {
      return a - b;
  }
  
  var n = ["10", "5", "40", "25", "100", "1"];
  document.write(n.sort(sortNumber));
  &lt;/script&gt;
  </code></pre>
  
  <p>The output then comes as:-</p>
  
  <pre><code>1,5,10,25,40,100
  </code></pre>
  
  <p>Now what I didn't understand is that why is this occurring &amp; can anybody please tell in details as to what type of algorithm is being used in this "<strong>sort()</strong>" function? This is because for any other language, I didn't find this problem where the function didn't sort the <em>numbers</em> correctly.  </p>
  
  <p>Any help is greatly appreciated.</p>