Back
Syntax
Study
Editor
Mode:
HTML
CSS
JavaScript
PHP
Reset
Run »
HTML / CSS / JS
$("th[data-sort]").css("cursor","pointer").on("click", function() { const col = $(this).data("sort"); const asc = $(this).data("asc") !== true; $(this).data("asc", asc).text($(this).text().replace(/[▲▼]/, "") + (asc ? " ▲" : " ▼")); const $tbody = $(this).closest("table").find("tbody"); const rows = $tbody.find("tr").toArray(); rows.sort((a, b) => { const av = $(a).find(`td[data-col=${col}]`).text(); const bv = $(b).find(`td[data-col=${col}]`).text(); return asc ? av.localeCompare(bv, undefined, { numeric: true }) : bv.localeCompare(av, undefined, { numeric: true }); }); $tbody.append(rows); });
Result
Open