JSFiddle - React, Tailwind, and code Playground
HTML
<table>
<tr data-val="1"><td>First</td></tr>
<tr data-val="6"><td>Third</td></tr>
<tr data-val="4"><td>Second</td></tr>
<tr data-val="10"><td>Last</td></tr>
</table>
JavaScript
$(document).ready(function() {
$('table').html(function() {
return $(this).find('tr').sort(function(a, b) {
return $(a).data('val') > $(b).data('val');
});
});
var $newRow = $('<tr data-val="5"><td>Should be placed between second and third!</td></tr>');
var elementToInsertBefore = $('tr').filter(function() {
return $(this).data('val') >= $newRow.data('val');
}).get(0);
if (typeof elementToInsertBefore === 'undefined') {
$('table').append($newRow);
} else {
$newRow.insertBefore(elementToInsertBefore);
}
});