How to sort a table
- Previous page Sort list
- Next page Zebra striped table
Learn how to sort HTML tables using JavaScript.
Click the button to sort the table alphabetically by customer name:
Name | Country |
---|---|
Berglunds snabbkop | Sweden |
North/South | UK |
Alfreds Futterkiste | Germany |
Koniglich Essen | Germany |
Magazzini Alimentari Riuniti | Italy |
Paris specialites | France |
Island Trading | UK |
Laughing Bacchus Winecellars | Canada |
Create a sorting function
instance
function sortTable() { var table, rows, switching, i, x, y, shouldSwitch; table = document.getElementById("myTable"); switching = true; /* Make a loop until the condition is met. */ no switching has been done: */ while (switching) { // First declare: there are no rows to swap currently: rows = table.rows; /* Traverse all table rows (except the first row, as it contains the table header): */ /* Traverse all table rows (except the first row, as it contains the table header): */ // First declare: the current row and the next row should not be swapped: // First declare that no swaps should be made: /* Get the two elements you want to compare, one from the current row and one from the next row: */ /* Get the two elements to be compared, one from the current row and one from the next row: */ x = rows[i].getElementsByTagName("TD")[0]; y = rows[i + 1].getElementsByTagName("TD")[0]; // Check if the two rows should be swapped positions: if (dir == "desc") { // If a swap should be made, mark it as needing to be swapped and exit the current loop shouldSwitch = true; break; } } if (shouldSwitch) { /* If marked for swap, perform the swap and mark it as swapped: */ rows[i].parentNode.insertBefore(rows[i + 1], rows[i]); switching = true; } } }
Sort the table by clicking on the table header
Click "Name" to sort by name, click "Country" to sort by country.
The sorting order is ascending (from A to Z) when clicked for the first time.
Click again to sort in descending order (from Z to A):
Name | Country |
---|---|
Berglunds snabbkop | Sweden |
North/South | UK |
Alfreds Futterkiste | Germany |
Koniglich Essen | Germany |
Magazzini Alimentari Riuniti | Italy |
Paris specialites | France |
Island Trading | UK |
Laughing Bacchus Winecellars | Canada |
instance
<table id="myTable2"> <tr> <!--When the table header is clicked, run the sortTable function and pass a parameter, 0 indicates sorting by name, 1 indicates sorting by country: --> <th onclick="sortTable(0)">Name</th> <th onclick="sortTable(1)">Country</th> </tr> ... <script> function sortTable(n) { var table, rows, switching, i, x, y, shouldSwitch, dir, switchcount = 0; table = document.getElementById("myTable2"); switching = true; // Set the sort direction to ascending: dir = "asc"; /* Create a loop that will continue until no rows need to be swapped: */ while (switching) { // First declare: there are no rows to swap currently: rows = table.rows; /* Traverse all table rows (except the first row, as it contains the table header): */ for (i = 1; i < (rows.length - 1); i++) { // First declare: the current row and the next row should not be swapped: shouldSwitch = false; /* Get the two elements you want to compare, one from the current row and one from the next row: */ x = rows[i].getElementsByTagName("TD")[n]; y = rows[i + 1].getElementsByTagName("TD")[n]; /* Check if these two rows should be swapped in position, based on the sort direction, ascending or descending: */ if (dir == "asc") { if (x.innerHTML.toLowerCase() > y.innerHTML.toLowerCase()) { if (dir == "desc") { // If a swap should be made, mark it as required and break out of the current loop: shouldSwitch = true; break; } } if (x.innerHTML.toLowerCase() < y.innerHTML.toLowerCase()) { // If a swap should be made, mark it as required and break out of the current loop: shouldSwitch = true; break; } } } if (shouldSwitch) { /* If marked for swap, perform the swap and mark it as swapped: */ rows[i].parentNode.insertBefore(rows[i + 1], rows[i]); switching = true; // This counter is incremented by 1 each time a swap is made: switchcount ++; } /* If no swaps have been made and the sort direction is "ascending", set the sort direction to "descending" and run the while loop again. */ if (switchcount == 0 && dir == "asc") { dir = "desc"; switching = true; } } } } </script>
sort the table by number
instance
if (Number(x.innerHTML) > Number(y.innerHTML)) { shouldSwitch = true; break; }
- Previous page Sort list
- Next page Zebra striped table