I was try sort the balance in table an ascending and an descending, but can’t. I looked the w3schools tutorial and theme in stackoverflow about insertBefore and insertAfter without libraries. But isn’t works.
My code:
function sortTableByBalance() {
let table, rows, switching, i, x, y, shouldSwitch;
table = document.getElementById("myTable");
switching = true;
while (switching) {
switching = false;
rows = table.rows;
for (i = 1; i < rows.length - 1; i++) {
shouldSwitch = false;
x = rows[i].getElementsByTagName("TD")[2];
y = rows[i + 1].getElementsByTagName("TD")[2];
if (Number(x.innerHTML) > Number(y.innerHTML)) {
shouldSwitch = true;
break;
} else if (Number(x.innerHTML) < Number(y.innerHTML)) {
shouldSwitch = false;
break;
}
}
// function insertAfter(prev, next) {
// const parent = prev.parentNode;
// parent.insertBefore(next, prev.nextSibling);
// return parent;
// }
if (shouldSwitch) {
rows[i].parentNode.insertBefore(rows[i + 1], rows[i]);
switching = true;
}
// else if (!shouldSwitch) {
// insertAfter(rows[i], rows[i + 1]);
// switching = true;
// }
}
}