JSFiddle - React, Tailwind, and code Playground

by Tintu Raju

HTML

<table name="mytable" id="mytable">
  <thead>
    <tr><th>Names</th></tr>
  </thead>
  <tbody>
    <tr><td>Ball</td></tr>
    <tr><td>Apple</td></tr>
    <tr><td>Cat</td></tr>
  </tbody>
</table>

JavaScript

function sortTable(table, order) {
    var asc   = order === 'asc',
        tbody = table.find('tbody');
    
    tbody.find('tr').sort(function(a, b) {
        if (asc) {
            return $('td:first', a).text().localeCompare($('td:first', b).text());
        } else {
            return $('td:first', b).text().localeCompare($('td:first', a).text());
        }
    }).appendTo(tbody);
}

sortTable($('#mytable'),'asc');