title: Sort table rows by ASC, DESC (different type of content per column)
body: <p>I have this function:</p>
  <pre><code>function sortTable(index) {
    var table, rows, switching, i, x, y, shouldSwitch, array = [];
    table = $(&quot;table&quot;);
    switching = true;
    
    while (switching) {
      
      switching = false;
      rows = table.find(&quot;tbody tr:not(.table-extra-hidden)&quot;);
  
      
      for (i = 0; i &lt; (rows.length); i++) {
        
        shouldSwitch = false;
        
        x = rows.eq(i).find(&quot;td&quot;).eq(index);
        y = rows.eq(i + 1).find(&quot;td&quot;).eq(index);
        
        if(sortOrder == &quot;desc&quot; &amp;&amp; x.find('.primary-info').text().replace(/[^a-zA-Z0-9 _]/g,'') &lt; y.find('.primary-info').text().replace(/[^a-zA-Z0-9 _]/g,''))
        {
          shouldSwitch = true;
          break;
        }
        else
        {
          shouldSwitch = true;
          break;
        }
      }
      if (shouldSwitch) {
        
        if(sortOrder == &quot;asc&quot;)
        {
          rows.eq(i).insertBefore(rows.eq(i-1));
        }
        else
        {
          rows.eq(i).insertAfter(table.find(&quot;tr:not(.table-extra-hidden)&quot;).last());
        }
        switching = true;
      }
    }
  }
  </code></pre>
  <p>The click event:</p>
  <pre><code>$(window).on(&quot;load&quot;, function(){
    $(&quot;a&quot;).click(function(e){
      if($(this).attr(&quot;href&quot;) == &quot;#&quot;)
      {
        e.preventDefault();
        e.stopPropagation();
      }
  
      if($(this).closest(&quot;th&quot;).length)
      {
        $(&quot;th&quot;).not($(this).closest(&quot;th&quot;)).find(&quot;a&quot;).removeClass(&quot;is-sorted sort-desc sort-asc&quot;)
        if($(this).hasClass(&quot;is-sorted&quot;))
        {
          if($(this).hasClass(&quot;sort-desc&quot;))
          {
            $(this).removeClass(&quot;sort-desc&quot;).addClass(&quot;sort-asc&quot;);
          }
          else
          {
            $(this).removeClass(&quot;sort-asc&quot;).addClass(&quot;sort-desc&quot;);
          }
        }
        else
        {
          $(this).addClass(&quot;is-sorted sort-desc&quot;);
        }
  
        //get table column index.
        var index = $(this).closest(&quot;th&quot;).index();
  
        var switching, table, rows, x, y, shouldSwitch;
  
        if($(this).hasClass(&quot;sort-desc&quot;))
        {
          sortOrder = &quot;desc&quot;;
        }
        else if($(this).hasClass(&quot;sort-asc&quot;))
        {
          sortOrder = &quot;asc&quot;;
        }
        sortTable(index);
      }
  }
  </code></pre>
  <p>It's used to sort this table:</p>
  <pre><code>&lt;table&gt;
      &lt;thead&gt;
        &lt;tr&gt;
          &lt;th&gt;&lt;a href=&quot;#&quot;&gt;Date&lt;/a&gt;&lt;/th&gt;
          &lt;th&gt;&lt;a href=&quot;#&quot;&gt;Amount&lt;/a&gt;&lt;/th&gt;
          &lt;th&gt;&lt;a href=&quot;#&quot;&gt;Trade&lt;/a&gt;&lt;/th&gt;
          &lt;th class=&quot;no-pointer&quot;&gt;&lt;/th&gt;
        &lt;/tr&gt;
      &lt;/thead&gt;
      &lt;tbody&gt;
        &lt;tr data-paired=&quot;1&quot; tabindex=&quot;0&quot;&gt;
          &lt;td&gt;
            &lt;b&gt;Bitcoin Buy&lt;/b&gt;
            &lt;br /&gt;
            &lt;span class=&quot;primary-info&quot;&gt;Jan 21, 2022&lt;/span&gt;
          &lt;/td&gt;
          &lt;td&gt;
            &lt;b class=&quot;primary-info&quot;&gt;$1,800.00&lt;/b&gt;
            &lt;br&gt;
            &lt;span&gt;0.00035678 BTC at $34,557.80&lt;/span&gt;
          &lt;/td&gt;
          &lt;td&gt;
            &lt;span class=&quot;primary-info&quot;&gt;Trade&lt;/span&gt;
          &lt;/td&gt;
          &lt;td class=&quot;expandable-trigger&quot;&gt;
            &lt;a href=&quot;#&quot;&gt;
              &lt;span class=&quot;icon icon-sm&quot; data-icon-name=&quot;chevron-down&quot;&gt;&lt;/span&gt;
            &lt;/a&gt;
          &lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr class=&quot;table-extra-hidden&quot; data-paired=&quot;1&quot;&gt;
          &lt;td&gt;
            &lt;b&gt;Status&lt;/b&gt;
            &lt;br&gt;
            &lt;span&gt;Filled&lt;/span&gt;
            &lt;br&gt;&lt;br&gt;
            &lt;b&gt;Filed&lt;/b&gt;
            &lt;br&gt;
            &lt;span&gt;Jan 1, 2022, 6:31 AM EST&lt;/span&gt;
          &lt;/td&gt;
          &lt;td&gt;
            &lt;b&gt;Paid With&lt;/b&gt;
            &lt;br&gt;
            &lt;span&gt;20.00000000 GUSD&lt;/span&gt;
          &lt;/td&gt;
          &lt;td&gt;&lt;/td&gt;
          &lt;td&gt;&lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr data-paired=&quot;2&quot; tabindex=&quot;0&quot;&gt;
          &lt;td&gt;
            &lt;b class=&quot;buy-option&quot;&gt;Dogecoin Buy&lt;/b&gt;
            &lt;br /&gt;
            &lt;span class=&quot;primary-info&quot;&gt;Jan 20, 2022&lt;/span&gt;
          &lt;/td&gt;
          &lt;td&gt;
            &lt;b class=&quot;primary-info&quot;&gt;$50.00&lt;/b&gt;
            &lt;br&gt;
            &lt;span class=&quot;converstion-option&quot;&gt;0.00035678 BTC at $34,557.80&lt;/span&gt;
          &lt;/td&gt;
          &lt;td&gt;
            &lt;span class=&quot;primary-info&quot;&gt;Trade&lt;/span&gt;
          &lt;/td&gt;
          &lt;td class=&quot;expandable-trigger&quot;&gt;
            &lt;a href=&quot;#&quot;&gt;
              &lt;span class=&quot;icon icon-sm&quot; data-icon-name=&quot;chevron-down&quot;&gt;&lt;/span&gt;
            &lt;/a&gt;
          &lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr data-paired=&quot;3&quot; tabindex=&quot;0&quot;&gt;
          &lt;td&gt;
            &lt;b class=&quot;buy-option&quot;&gt;Bitcoin Trade&lt;/b&gt;
            &lt;br /&gt;
            &lt;span class=&quot;primary-info&quot;&gt;Jan 21, 2022&lt;/span&gt;
          &lt;/td&gt;
          &lt;td&gt;
            &lt;b class=&quot;primary-info&quot;&gt;$1,200.00&lt;/b&gt;
            &lt;br&gt;
            &lt;span class=&quot;converstion-option&quot;&gt;0.00035678 BTC at $34,557.80&lt;/span&gt;
          &lt;/td&gt;
          &lt;td&gt;
            &lt;span class=&quot;primary-info&quot;&gt;ACH Deposit&lt;/span&gt;
          &lt;/td&gt;
          &lt;td class=&quot;expandable-trigger&quot;&gt;
            &lt;a href=&quot;#&quot;&gt;
              &lt;span class=&quot;icon icon-sm&quot; data-icon-name=&quot;chevron-down&quot;&gt;&lt;/span&gt;
            &lt;/a&gt;
          &lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr data-paired=&quot;4&quot; tabindex=&quot;0&quot;&gt;
          &lt;td&gt;
            &lt;b class=&quot;buy-option&quot;&gt;Bitcoin Interest Paid&lt;/b&gt;
            &lt;br /&gt;
            &lt;span class=&quot;primary-info&quot;&gt;Dec 15, 2021&lt;/span&gt;
          &lt;/td&gt;
          &lt;td&gt;
            &lt;b class=&quot;primary-info&quot;&gt;$1,900.00&lt;/b&gt;
            &lt;br&gt;
            &lt;span class=&quot;converstion-option&quot;&gt;0.00035678 BTC at $34,557.80&lt;/span&gt;
          &lt;/td&gt;
          &lt;td&gt;
            &lt;span class=&quot;primary-info&quot;&gt;Interest Payment&lt;/span&gt;
          &lt;/td&gt;
          &lt;td class=&quot;expandable-trigger&quot;&gt;
            &lt;a href=&quot;#&quot;&gt;
              &lt;span class=&quot;icon icon-sm&quot; data-icon-name=&quot;chevron-down&quot;&gt;&lt;/span&gt;
            &lt;/a&gt;
          &lt;/td&gt;
        &lt;/tr&gt;
        &lt;tr data-paired=&quot;5&quot; tabindex=&quot;0&quot;&gt;
          &lt;td&gt;
            &lt;b class=&quot;buy-option&quot;&gt;Deposit from Chase&lt;/b&gt;
            &lt;br /&gt;
            &lt;span class=&quot;primary-info&quot;&gt;Dec 1, 2021&lt;/span&gt;
          &lt;/td&gt;
          &lt;td&gt;
            &lt;b class=&quot;primary-info&quot;&gt;$800.00&lt;/b&gt;
            &lt;br&gt;
            &lt;span class=&quot;converstion-option&quot;&gt;0.00035678 BTC at $34,557.80&lt;/span&gt;
          &lt;/td&gt;
          &lt;td&gt;
            &lt;span class=&quot;primary-info&quot;&gt;Trade&lt;/span&gt;
          &lt;/td&gt;
          &lt;td class=&quot;expandable-trigger&quot;&gt;
            &lt;a href=&quot;#&quot;&gt;
              &lt;span class=&quot;icon icon-sm&quot; data-icon-name=&quot;chevron-down&quot;&gt;&lt;/span&gt;
            &lt;/a&gt;
          &lt;/td&gt;
        &lt;/tr&gt;
      &lt;/tbody&gt;
    &lt;/table&gt;
  </code></pre>
  <p>There are two things:</p>
  <p>Firstly, a column when sorted is finding the class name &quot;primary-info&quot; to sort from that data point (vs everything) and secondly each column has different data points that can be sorted by. This doesn't seem to be working and just infinitely looping instead sorting and stopping. What am I missing here?</p>