Sorting table rows with jQuery
Let’s say you have the following table on the page and you need to sort it alphabetically:
<table>
<tr><td>Beta</td><td>2</td></tr>
<tr><td>Omega</td><td>4</td></tr>
<tr><td>Aplha</td><td>1</td></tr>
<tr><td>Gamma</td><td>3</td></tr>
</table>
The following jQuery script does the job:
$(document).ready( function () {
tbody = $('table').find('tbody');
tbody.find('tr').sort(function(a, b) {
return $('td:first', a).text().localeCompare($('td:first', b).text());
}).appendTo(tbody);
});
Do not forget to add jQuery itself to the page:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
The result can be seen here:
https://jsfiddle.net/1uo687yn/