title: Weird behavior in sorting JavaScript array
body: <p>I encountered a strange behavior while trying to sort a JavaScript array.</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="false" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>var arr = ['a', 'b', 'C', 'd', 'e', 'f', 'g', 'h', 'I', 'k'];
  
  arr.sort(function (a, b) {
    console.log(a, b);
    if (a.length &lt; b.length) return 1;
    else if (a.length &gt; b.length) return -1;
    else return 0;
  });</code></pre>
  </div>
  </div>
  </p>
  
  <p>Works fine in this case giving me back the same array.</p>
  
  <p>The console goes like this,</p>
  
  <p><a href="https://i.stack.imgur.com/b40hn.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/b40hn.png" alt="enter image description here"></a></p>
  
  <p>But when I try for this below input,</p>
  
  <pre><code>var arr = ['a', 'b', 'C', 'd', 'e', 'f', 'g', 'h', 'I', 'k', 'l'];
  </code></pre>
  
  <p>Gives me this,</p>
  
  <p><a href="https://i.stack.imgur.com/w6C2A.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/w6C2A.png" alt="enter image description here"></a></p>
  
  <p>I can't quite figure out why that is happening.</p>
  
  <p>PS. I am writing this custom sort checking the length of the elements because I need an array that has its elements sorted according to the length.</p>