JSFiddle - React, Tailwind, and code Playground
HTML
<html> <head>
<title>Example : sort table rows</title>
</head>
<body>
<div id="main" class='column'>
<h1>jQuery example: sorting rows within groups</h1>
<input id='btn1' type='button' value='Sort it'></input>
<table class="sortable">
<thead>
<tr> <th></th> <th>A-head</th> <th>B-head</th> <th>C-head</th> </tr>
</thead>
<tfoot>
<tr> <td></td> <td>A-foot</td> <td>B-foot</td> <td>C-foot</td></tr>
</tfoot>
<tbody class='sortable'>
<tr> <td>DDD</td><td> r1c1</td> <td>r1c2</td> <td>r1c3</td> </tr>
<tr> <td>AAA</td><td> r2c1</td> <td>r2c2</td> <td>r2c3</td> </tr>
<tr> <td>CCC</td><td> r3c1</td> <td>r3c2</td> <td>r3c3</td> </tr>
<tr> <td>BBB</td><td> r4c1</td> <td>r4c2</td> <td>r4c3</td> </tr>
<tr> <td colspan="4">summary info for the first group of rows</td> </tr>
</tbody>
<tbody class='sortable'>
<tr> <td>Zorro</td><td> r5c1</td> <td>r5c2</td> <td>r5c3</td> </tr>
<tr> <td>Caleb</td><td> r6c1</td> <td>r6c2</td> <td>r6c3</td> </tr>
<tr> <td>Momo</td><td> r7c1</td> <td>r7c2</td> <td>r7c3</td> </tr>
<tr> <td>Wolfie</td><td> r8c1</td> <td>r8c2</td> <td>r8c3</td> </tr>
<tr> <td colspan="4">summary info for rowgroup #2</td> </tr>
</tbody>
<tbody class='sortable'>
<tr> <td>Ulysses</td><td> r9c1</td> <td>r9c2</td> <td>r9c3</td> </tr>
<tr> <td>Penelope</td><td> r10c1</td> <td>r10c2</td> <td>r10c3</td> </tr>
<tr> <td>Edvald</td><td> r11c1</td> <td>r11c2</td> <td>r11c3</td> </tr>
<tr> <td>Jan</td><td> r12c1</td> <td>r12c2</td> <td>r12c3</td> </tr>
<tr> <td colspan="4">summary info for the last group of rows</td> </tr>
</tbody>
</table>
</div>
</body></html>
CSS
* { padding: 0; margin: 0; }
html, body { height: 100%; }
body, textarea {
font-family: "lucida sans", verdana, arial, helvetica, sans-serif;
font-size: 80%;
}
textarea { font-size: 100%;}
h1 { font-size: 1.9em; font-weight: bold; padding: 10px 10px 0; }
h2 { font-size: 1.5em; font-weight: bold; padding: 10px 10px 0; color:Navy;}
p { margin-top: 6px; padding: 0 10px 1em; }
th, td { border: 1px solid black; padding:2px; margin: 0px 4px 0px 4px; }
thead th, tfoot td { background-color: #CCC; }
input { padding: 2px 8px;}
div.column {
border: 1px dotted #666;
background-color: #E6E6FA;
position: absolute;
top: 20px; /* margin from top */
bottom: 20px; /* margin from bottom */
padding: 8px;
}
* html {
height: 100%;
}
JavaScript
function SortIt() {
jQuery('table.sortable > tbody.sortable').each(function(index,tbody) {
var $rowGroup = jQuery(tbody);
// select all but the last row in the tbody
var rows = $rowGroup.find('tr:not(last-child)').get();
var sortDirection = $rowGroup.is('.sorted-asc') ? -1 : 1;
// Set a custom property on each row - 'sortKey', the key to sort.
// This example uses the text in the first column. It could use
// any column, or any content in the row.
jQuery.each(rows, function(index, row) {
row.sortKey = jQuery(row).children('td').first().text();
});
// actually sort the rows
rows.sort(function(a, b) {
if (a.sortKey < b.sortKey) return -sortDirection;
if (a.sortKey > b.sortKey) return sortDirection;
return 0;
});
// retain the summary row - the last one
var summaryRow = $rowGroup.find("tr:last-child");
// remove all the rows from the tbody
$rowGroup.find("tr").remove();
// append the rows in sorted order
jQuery.each(rows, function(index, row) {
$rowGroup.append(row);
row.sortKey = null;
});
// append the final row
$rowGroup.append(summaryRow);
if (sortDirection == 1) { $rowGroup.addClass('sorted-asc'); }
else {$rowGroup.removeClass('sorted-asc'); }
});
}
$(document).ready( function (){
jQuery('#btn1').click(SortIt);
});