title: Sort table based on version number
body: <p>I want to sort a table based on version number with the highest at the top. If the cell starts with "v" followed by a digit it's considered a version number. Otherwise it should be sorted at the bottom of the table alphabetically.</p>
  
  <p>The result is currently exactly the way I want it, except for v2.0 should come before v2a, and v2a should come before v2b. I know that my <code>parseFloat()</code> is messing that up, but how can I do this in a better way?</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>function sortTable(table, order, selector, target) {
   selector = selector || 'th:first-child, td:first-child';
   var asc = order === 'asc';
   target = target || '';
   
   var tbody = table.querySelector('tbody') || table;
   var nodes = tbody.querySelectorAll('tr');
   var sortedNodes = Array.prototype.slice.apply(nodes);
   sortedNodes.sort(function (a, b) {
    var textA = a.querySelector(selector).textContent;
    var textB = b.querySelector(selector).textContent;
    if( target == 'versions' )
    {
     if (textA.match(/^v\d.*/i)) {
      textA = parseFloat(textA.substr(1));
     }
     if (textB.match(/^v\d.*/i)) {
      textB = parseFloat(textB.substr(1));
     }
    
    }
    result = textA - textB;
    if (isNaN(result))
    {
     return (asc &amp;&amp; textB.toString() != '' ) ? textA.toString().localeCompare(textB) : textB.toString().localeCompare(textA);
    }
    else
    {
     return (asc) ? -result : result;
    }
   });
   tbody.textContent = '';
   for (var i = 0; i &lt; sortedNodes.length; i++) {
    tbody.appendChild(sortedNodes[i]);
   }
  }
  sortTable(document.getElementById('versions'), 'asc', '', 'versions');</code></pre>
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;table id="versions"&gt;
   &lt;tbody&gt;
    &lt;tr&gt;&lt;td&gt;Text&lt;/td&gt;&lt;/tr&gt;
    &lt;tr&gt;&lt;td&gt;v2b&lt;/td&gt;&lt;/tr&gt;
    &lt;tr&gt;&lt;td&gt;v3.0&lt;/td&gt;&lt;/tr&gt;
    &lt;tr&gt;&lt;td&gt;v2a&lt;/td&gt;&lt;/tr&gt;
    &lt;tr&gt;&lt;td&gt;&lt;/td&gt;&lt;/tr&gt;
    &lt;tr&gt;&lt;td&gt;v2.0&lt;/td&gt;&lt;/tr&gt;
    &lt;tr&gt;&lt;td&gt;No version number&lt;/td&gt;&lt;/tr&gt;
    &lt;tr&gt;&lt;td&gt;v1.0 Work in Progress&lt;/td&gt;&lt;/tr&gt;
   &lt;/tbody&gt;
  &lt;/table&gt;</code></pre>
  </div>
  </div>
  </p>