datatable: sort numbers numerically

This commit is contained in:
2025-02-23 00:54:33 +01:00
parent a92cd65bad
commit 9e5c32850b

View File

@ -36,16 +36,28 @@ function sortTable(n) {
one from current row and one from the next: */ one from current row and one from the next: */
x = rows[i].getElementsByTagName("TD")[n]; x = rows[i].getElementsByTagName("TD")[n];
y = rows[i + 1].getElementsByTagName("TD")[n]; y = rows[i + 1].getElementsByTagName("TD")[n];
xval = x.innerHTML;
yval = y.innerHTML;
if (!isNaN(parseInt(xval))) {
xval = parseInt(xval);
} else {
xval = xval.toLowerCase();
}
if (!isNaN(parseInt(yval))) {
yval = parseInt(yval);
} else {
yval = yval.toLowerCase();
}
/* Check if the two rows should switch place, /* Check if the two rows should switch place,
based on the direction, asc or desc: */ based on the direction, asc or desc: */
if (dir == "asc") { if (dir == "asc") {
if (x.innerHTML.toLowerCase() > y.innerHTML.toLowerCase()) { if (xval > yval) {
// If so, mark as a switch and break the loop: // If so, mark as a switch and break the loop:
shouldSwitch = true; shouldSwitch = true;
break; break;
} }
} else if (dir == "desc") { } else if (dir == "desc") {
if (x.innerHTML.toLowerCase() < y.innerHTML.toLowerCase()) { if (xval < yval) {
// If so, mark as a switch and break the loop: // If so, mark as a switch and break the loop:
shouldSwitch = true; shouldSwitch = true;
break; break;