title: Strange sorting of array when using Array.prototype.sort in Chrome
body: <p>I have found an oddity when using <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort" rel="nofollow"><code>Array.prototype.sort()</code></a> on an array of numbers and I'm not sure what's causing it.</p>
  
  <p>My goal is to reverse an array using <code>sort</code> (not using <code>reverse</code>) so I can chain it like so:</p>
  
  <pre class="lang-js prettyprint-override"><code>const shouldReverse = Math.random() &gt; 0.5,
        result = foo().bar().map(...).reverseIf(shouldReverse);
  </code></pre>
  
  <p>I believe I should be able to achieve this using <code>sort</code>, which seems to work in some cases but not others.</p>
  
  <p>Here is a working example:</p>
  
  <pre class="lang-js prettyprint-override"><code>const myArray = ['a', 'b', 'c', 'd'],
        mySortedArray = myArray.sort(() =&gt; 1); 
  
  console.log(mySortedArray);
  </code></pre>
  
  <pre class="lang-js prettyprint-override"><code>["d", "c", "b", "a"]
  </code></pre>
  
  <p>And a non-working example:</p>
  
  <pre class="lang-js prettyprint-override"><code>const myArray = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k'],
        mySortedArray = myArray.sort(() =&gt; 1); 
  
  console.log(mySortedArray);
  </code></pre>
  
  <pre class="lang-js prettyprint-override"><code>["f", "k", "a", "j", "i", "h", "g", "b", "e", "d", "c"]
  </code></pre>
  
  <p>This only happens in Chrome and only when there are more than 10 elements in the array  — could it be some form of optimisation in Chrome's V8 engine?</p>