title: Javascript: Sort an array of String numbers
body: <p>I have an array of school grades that look like the following</p>
  
  <p>(Note 'N' stands for no grades and 'K' stands for kindergarden)</p>
  
  <blockquote>
    <p>const toSort = ['1','3','4','5','6','7','9','10','11','12','K','2','N','8'];</p>
  </blockquote>
  
  <p>Using the javascript sort() method I would like to arrange the array so it will look like </p>
  
  <blockquote>
    <p>const sorted = ['K','1','2','3','4','5','6','7','8','9','10','11','12','N'];</p>
  </blockquote>
  
  <p>Here is my attempt at it:</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>const toSort = ['1', '3', '4', '5', '6', '7', '9', '10', '11', '12', 'K', '2', 'N', '8'];
  
  toSort.sort();
  // Produces: ["1", "10", "11", "12", "2", "3", "4", "5", "6", "7", "8", "9", "K", "N"]
  
  const test = toSort.sort((a, b) =&gt; {
    if (a === 'K') {
      return -1;
    }
  
    return Number(a) &lt; Number(b) ? -1 : Number(a) &gt; Number(b) ? 1 : 0;
  });
  
  console.log(test)</code></pre>
  </div>
  </div>
  </p>
  
  <p><a href="https://jsbin.com/pocajayala/1/edit?html,js,console,output" rel="noreferrer">https://jsbin.com/pocajayala/1/edit?html,js,console,output</a></p>
  
  <p>Any idea how I can resolve this?</p>