Advertisement
AviEzerzer

Untitled

Jan 30th, 2020
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function sortTable(n) {
  2.     var table,
  3.         rows,
  4.         switching,
  5.         i,
  6.         x,
  7.         y,
  8.         shouldSwitch,
  9.         dir,
  10.         switchcount = 0;
  11.     table = document.getElementById('myTable');
  12.     switching = true;
  13.     //Set the sorting direction to ascending:
  14.     dir = 'asc';
  15.     /*Make a loop that will continue until no switching has been done:*/
  16.     while (switching) {
  17.         //start by saying: no switching is done:
  18.         switching = false;
  19.         rows = table.getElementsByTagName('TR');
  20.         /*Loop through all table rows (except the first, which contains table headers):*/
  21.         for (i = 1; i < rows.length - 1; i++) {
  22.             //Change i=0 if you have the header th a separate table.
  23.             //start by saying there should be no switching:
  24.             shouldSwitch = false;
  25.             /*Get the two elements you want to compare,one from current row and one from the next:*/
  26.             x = rows[i].getElementsByTagName('TD')[n];
  27.             y = rows[i + 1].getElementsByTagName('TD')[n];
  28.  
  29.             const xNum = typeCheck(x.innerHTML);
  30.             const yNum = typeCheck(y.innerHTML);
  31.             console.log('TCL: ynum', i, yNum);
  32.             console.log('TCL: xnum', i, xNum);
  33.  
  34.             /*check if the two rows should switch place,based on the direction, asc or desc:*/
  35.             if (dir == 'asc') {
  36.                 if (Number(xNum) > Number(yNum)) {
  37.                     //if (xNum.toLowerCase() > yNum.toLowerCase()) {
  38.                     //if so, mark as a switch and break the loop:
  39.                     shouldSwitch = true;
  40.                     break;
  41.                 }
  42.             } else if (dir == 'desc') {
  43.                 if (Number(xNum) < Number(yNum)) {
  44.                     //if (xNum.toLowerCase() < yNum.toLowerCase()) {
  45.                     //if so, mark as a switch and break the loop:
  46.                     shouldSwitch = true;
  47.                     break;
  48.                 }
  49.             }
  50.         }
  51.         if (shouldSwitch) {
  52.             /*If a switch has been marked, make the switch and mark that a switch has been done:*/
  53.             rows[i].parentNode.insertBefore(rows[i + 1], rows[i]);
  54.             switching = true;
  55.             //Each time a switch is done, increase this count by 1:
  56.             switchcount++;
  57.         } else {
  58.             /*If no switching has been done AND the direction is "asc",set the direction to "desc" and run the while loop again.*/
  59.             if (switchcount == 0 && dir == 'asc') {
  60.                 dir = 'desc';
  61.                 switching = true;
  62.             }
  63.         }
  64.     }
  65. }
  66.  
  67. function typeCheck(value) {
  68.     if (Number(value)) {
  69.         return value;
  70.     }
  71.  
  72.     const isDate = new Date(value);
  73.     if (isDate instanceof Date && !isNaN(isDate.valueOf())) {
  74.         return isDate.getTime();
  75.     }
  76.  
  77.     return value;
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement