I could really use a second pair of eyes from someone more versed in Javascript and sorting algorithms.
I'm trying to sort my table by date (mm/dd/yyyy)
This solution is working fantastic for ascending order, even on our larger data tables, but when I switch to the less-than sign for descending order, it works to a certain extent.
Smaller data sets work fine, but larger ones just brick the loop. I'm not sure what the disconnect is here. It is especially confusing, because ascending order works fine.
WebApp.sortDateReverse = function(colNam, colNum)
{
var table, rows, switching, i, x, y, shouldSwitch;
table = document.getElementById("myTable");
switching = true;
console.log('This is colNum', colNum);
console.log('This is colName', colNam);
/*Make a loop that will continue until
no switching has been done:*/
while (switching) {
//start by saying: no switching is done
switching = false;
rows = table.rows;
/*Loop through all table rows (except the
first, which contains table headers):*/
for(i = 1;i<(rows.length - 1);i++) {
//start by saying there should be no switching:
shouldSwitch = false;
console.log('This is i:', i);
console.log('This is row length:', rows.length);
/*Get the two elements you want to compare,
one from current row and one from the next:*/
x = rows[i].getElementsByTagName("TD")[colNum];
y = rows[i + 1].getElementsByTagName("TD")[colNum];
//check if the two rows should switch place:
if (WebApp.convertDate(x.innerHTML) < WebApp.convertDate(y.innerHTML)) {
//if so, mark as a switch and break the loop:
//console.log('Switching x:', x.innerHTML , 'with y:', y.innerHTML);
shouldSwitch = true;
break;
}
}
if (shouldSwitch) {
/*If a switch has been marked, make the switch
and mark that a switch has been done:*/
rows[i].parentNode.insertBefore(rows[i + 1], rows[i]);
switching = true;
}
}
};
WebApp.convertDate = function(d) {
return Date.parse(d)
};