title: How can I force certain strings to be sorted last in javascript?
body: <p>I'm trying to sort an array of strings alphabetically, except for certain strings (e.g. <code>"NA"</code> and <code>"Wild"</code>) that should always be placed last. The sorting priority should be <code>sorted_values_alphabetically &lt; "NA" &lt; "Wild"</code>.</p>
  
  <p>If we have the following array:</p>
  
  <pre><code>["Wild", "sit", "ipsum", "dolor", "NA", "amet", "lorem"];
  </code></pre>
  
  <p>I would like it to be sorted as:</p>
  
  <pre><code>["amet", "dolor", "ipsum", "lorem", "sit", "NA", "Wild"];
  </code></pre>
  
  <p>I was thinking something like</p>
  
  <pre><code>arr.sort(function(a,b) {
    var aVal = a, bVal = b;
  
    // Hack to make values &lt; "NA" &lt; "Wild"
    if (aVal == "NA") aVal = "zzz" + aVal;
    if (bVal == "NA") bVal = "zzz" + bVal;
    if (aVal == "Wild") aVal = "zzzz" + aVal;
    if (bVal == "Wild") bVal = "zzzz" + bVal;
  
    return aVal.toLowerCase().localeCompare(bVal.toLowerCase());
  });
  </code></pre>
  
  <p>But this probably wouldn't work for all Unicode characters.</p>
  
  <p>I'm also interested in performant algorithms!</p>
  
  <h3>Performance</h3>
  
  <p>Just FYI, T. J. Crowder's algorithm is slightly more performant via <a href="http://jsperf.com/http-stackoverflow-com-questions-24290131" rel="nofollow">jsPerf</a>. Altohugh I prefer Halcyon's more concise approach!</p>