Skip to content

Instantly share code, notes, and snippets.

@bendiksolheim
Created September 25, 2019 18:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bendiksolheim/307a6af9aba2c4360c75b900e484dd58 to your computer and use it in GitHub Desktop.
Save bendiksolheim/307a6af9aba2c4360c75b900e484dd58 to your computer and use it in GitHub Desktop.
Sort table descending by value in last row
const sortedRows = Array.from(document.querySelectorAll('tbody tr')).sort((a, b) => {
const aValue = parseInt(a.querySelector('td:last-child').innerText);
const bValue = parseInt(b.querySelector('td:last-child').innerText);
return bValue - aValue;
});
const tbody = document.querySelector('tbody');
tbody.innerHTML = '';
sortedRows.forEach(row => tbody.appendChild(row));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment