JSFiddle - React, Tailwind, and code Playground
HTML
<table id="myTable">
<tr>
<th>Year</th>
<th>Dollars</th>
</tr>
<tr>
<td>1985</td>
<td>4500.17</td>
</tr>
<tr>
<td>1986</td>
<td>1215.33</td>
</tr>
<tr>
<td>1987</td>
<td>1215.00</td>
</tr>
<tr>
<td>1988</td>
<td>3600.00</td>
</tr>
</table>
<button>Sort by Dollars</button>
CSS
table {
margin: 1ex;
border: 1px solid black;
}
td, th {
padding: 1ex;
border: 1px solid black;
}
th {
background: wheat;
}
JavaScript
//--- Activate the button.
$("button").click (DoSort)
function DoSort ()
{
//--- Get the table we want to sort.
var jTableToSort = $("table#myTable");
//--- Get the rows to sort, but skip the first row, since it contains column titles.
var jRowsToSort = jTableToSort.find ("tr:gt(0)");
//--- Sort the rows in place.
jRowsToSort.sort (SortBy2ndColumnNumber).appendTo (jTableToSort);
function SortBy2ndColumnNumber (zA, zB)
{
var ValA = parseFloat ($(zA).find ("td:eq(1)").text () );
var ValB = parseFloat ($(zB).find ("td:eq(1)").text () );
return ValA - ValB;
}
}