title: Sort table with empty cell
body: <p>How I want to make the empty cell will be on the top during asc order and at bottom during desc order. Here is my code</p>
  
  <pre><code>function sortTable(n) {
    var table, rows, switching, i, x, y, shouldSwitch, dir, switchcount = 0;
    table = document.getElementById("mytable");
    switching = true;
  
    dir = "asc"; 
  
    while (switching) {
       //start by saying: no switching is done:
       switching = false;
       rows = table.rows;
  
       for (i = 1; i &lt; (rows.length - 1); i++) {
          shouldSwitch = false;
          x = rows[i].getElementsByTagName("td")[n];
          y = rows[i + 1].getElementsByTagName("td")[n];
  
          if (dir == "asc") {
             if (x.innerHTML.toLowerCase() &gt; y.innerHTML.toLowerCase()) {
                shouldSwitch= true;
                break;
             }
          } else if (dir == "desc") {
             if (x.innerHTML.toLowerCase() &lt; y.innerHTML.toLowerCase()) {
                shouldSwitch = true;
                break;
             }
        }
      }
    if (shouldSwitch) {
       rows[i].parentNode.insertBefore(rows[i + 1], rows[i]);
       switching = true;
       switchcount ++;
    } else {
      if (switchcount == 0 &amp;&amp; dir == "asc") {
         dir = "desc";
         switching = true;
       }
    }
  }
  }
  </code></pre>
  
  <p>I had following the tuturial from this website </p>
  
  <p><a href="https://www.w3schools.com/howto/howto_js_sort_table.asp" rel="nofollow noreferrer">https://www.w3schools.com/howto/howto_js_sort_table.asp</a></p>
  
  <p>But the result is like this</p>
  
  <p>if ascending order</p>
  
  <pre><code>a
  &lt;empty&gt;
  &lt;empty&gt;
  &lt;empty&gt;
  z
  </code></pre>
  
  <p>if descending order</p>
  
  <pre><code>z
  &lt;empty&gt;
  &lt;empty&gt;
  &lt;empty&gt;
  a
  </code></pre>