Guest User

Untitled

a guest
Mar 21st, 2019
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <script>
  2. function sortTable() {
  3.   var table, rows, switching, i, x, y, shouldSwitch;
  4.   table = document.getElementById("myTable");
  5.   switching = true;
  6.   /*Make a loop that will continue until
  7.   no switching has been done:*/
  8.   while (switching) {
  9.     //start by saying: no switching is done:
  10.     switching = false;
  11.     rows = table.rows;
  12.     /*Loop through all table rows (except the
  13.     first, which contains table headers):*/
  14.     for (i = 1; i < (rows.length - 1); i++) { //start by saying there should be no switching: shouldSwitch = false; /*Get the two elements you want to compare, one from current row and one from the next:*/ x = rows[i].getElementsByTagName("TD")[0]; y = rows[i + 1].getElementsByTagName("TD")[0]; //check if the two rows should switch place: if (x.innerHTML.toLowerCase() > y.innerHTML.toLowerCase()) {
  15.         //if so, mark as a switch and break the loop:
  16.         shouldSwitch = true;
  17.         break;
  18.       }
  19.     }
  20.     if (shouldSwitch) {
  21.       /*If a switch has been marked, make the switch
  22.       and mark that a switch has been done:*/
  23.       rows[i].parentNode.insertBefore(rows[i + 1], rows[i]);
  24.       switching = true;
  25.     }
  26.   }
  27. }
  28. </script>
Add Comment
Please, Sign In to add comment