Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix buggy sorting in dataexplorer_table.php #212

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 34 additions & 59 deletions src/DataExplorer/dataexplorer_table.php
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,8 @@ function sortTable(idxToSort, order) {
lastsort = idxToSort
lastorder = order

if(lastorder=="asc"){
// Down arrow is supposed to be for descending order I guess, grom highest to lowest
if(lastorder=="desc"){
document.getElementById('col'+'0').innerHTML = "▽"
document.getElementById('col'+'1').innerHTML = "▽"
document.getElementById('col'+'2').innerHTML = "▽"
Expand All @@ -429,65 +430,39 @@ function sortTable(idxToSort, order) {
}


var table, rows, switching, i, x, y, shouldSwitch;
table = document.getElementById("myTable");
//console.log(table.innerHTML)
switching = true;
/*Make a loop that will continue until
no switching has been done:*/
var j = 0
while (switching) {
//start by saying: no switching is done:
switching = false;
rows = table.rows;
//console.log(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;
/*Get the two elements you want to compare,
one from current row and one from the next:*/
x = rows[i].getElementsByTagName("TD")[idxToSort];
y = rows[i + 1].getElementsByTagName("TD")[idxToSort];
//check if the two rows should switch place:
assess=false

if(idxToSort>0){
if(order=="desc"){
//console.log("___")
//console.log(idxToSort)
//console.log(x.getElementsByTagName('span')[1].innerHTML)

assess = (parseInt(x.getElementsByTagName('span')[1].innerHTML.toLowerCase()) > parseInt(y.getElementsByTagName('span')[1].innerHTML.toLowerCase()))
} else if(order=="asc") {
assess = (parseInt(x.getElementsByTagName('span')[1].innerHTML.toLowerCase()) < parseInt(y.getElementsByTagName('span')[1].innerHTML.toLowerCase()))
}
} else if(idxToSort==0){
if(order=="desc"){
//assess = (parseInt(x.innerHTML.toLowerCase()) < parseInt(y.innerHTML.toLowerCase()))
assess = (('' + x.getElementsByTagName('span')[0].innerHTML).localeCompare(y.getElementsByTagName('span')[0].innerHTML))
assess = (assess == 1)
} else if(order=="asc") {
//assess = (parseInt(x.innerHTML.toLowerCase()) > parseInt(y.innerHTML.toLowerCase()))
assess = (('' + y.getElementsByTagName('span')[0].innerHTML).localeCompare(x.getElementsByTagName('span')[0].innerHTML))
assess = (assess == 1)
}
}
if (assess==true) {
//if so, mark as a switch and break the loop:
shouldSwitch = true;
break;
}
}
var table, rows, x, y;
table = document.getElementById("myTable");

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;
}
}


function compareRows(row1,row2) {
x = row1.getElementsByTagName("TD")[idxToSort];
y = row2.getElementsByTagName("TD")[idxToSort];

if(idxToSort > 0){
// Put back decimal point and use ParseFloat, and take care of "+-0,0" value that caused a NaN
x = parseFloat(x.getElementsByTagName('span')[1].innerHTML.toLowerCase().replace(",",".").replace("+-", ""));
y = parseFloat(y.getElementsByTagName('span')[1].innerHTML.toLowerCase().replace(",",".").replace("+-", ""));
if(x == NaN) return 1;
if(y == NaN) return -1;

if(order == "desc")
return y - x;
else if(order == "asc")
return x - y;
}
else if(idxToSort == 0) {
if(order == "desc")
return (('' + y.getElementsByTagName('span')[0].innerHTML).localeCompare(x.getElementsByTagName('span')[0].innerHTML));
else if(order == "asc")
return (('' + x.getElementsByTagName('span')[0].innerHTML).localeCompare(y.getElementsByTagName('span')[0].innerHTML));

}
}

// Used built-it javascript sort function instead of risking using a handwritten algorithm that might have mistakes
rows = Array.from(table.rows).slice(1);
rows.sort(compareRows).forEach(function(r) {r.parentNode.appendChild(r);});
}
</script>

Expand Down