title: How to implement Array.prototype.sort default compare function?
body: <h2>Background</h2>
  
  <p>I need to implement a function with the same behaviour as the default compare function of Array.prototype.sort</p>
  
  <p><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort" rel="nofollow noreferrer">https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort</a></p>
  
  <p>After reading the documentation I stumbled upon this:</p>
  
  <blockquote>
    <p>The default sort order is according to string Unicode code points.</p>
  </blockquote>
  
  <p>What does this mean? Does it mean I convert every object to a string?</p>
  
  <p>If so, assuming I have the array <code>[2, "a", { hello: "world" }]</code> would these steps be correct?</p>
  
  <ol>
  <li>convert <code>[2, "a", { hello: "world" }]</code> to <code>["2", "a", '{ hello: "world" }']</code></li>
  <li>convert the first character of every string to a Number</li>
  <li>order by that number </li>
  </ol>
  
  <h2>Question</h2>
  
  <p>How do I implement a compare function that given any object, behaves exactly as the compare function from sort?</p>
  
  <h2>Notes</h2>
  
  <p>Reading the ECMA specification: </p>
  
  <ul>
  <li><a href="http://www.ecma-international.org/ecma-262/6.0/#sec-array.prototype.sort" rel="nofollow noreferrer">http://www.ecma-international.org/ecma-262/6.0/#sec-array.prototype.sort</a></li>
  </ul>
  
  <p>I now believe that if <code>comparefn</code> is <code>undefined</code>, then they use this algorithm as a default:</p>
  
  <p><a href="http://www.ecma-international.org/ecma-262/6.0/#sec-sortcompare" rel="nofollow noreferrer">http://www.ecma-international.org/ecma-262/6.0/#sec-sortcompare</a></p>
  
  <p>Can someone confirm?</p>