useful-forks / useful-forks.github.io

Improving GitHub's Forks list discoverability through automatic filtering. The project offers an online tool and a Chrome extension.
https://useful-forks.github.io/
MIT License
1.19k stars 61 forks source link

Sorting algorithm is wrong #1

Closed payne911 closed 4 years ago

payne911 commented 4 years ago

The very first row to be appended to the table is not taken into account by the current sorting algorithm:

image

The algo that is found in 2 different places (the Chrome Extension, and the Website):

function getTdValue(rows, index, col) {
  return rows.item(index).getElementsByTagName('td').item(col).getAttribute("value");
}

/** 'sortColumn' index starts at 0.   https://stackoverflow.com/a/37814596/9768291 */
function sortTable(table_id, sortColumn){
  let tableData = document.getElementById(table_id).getElementsByTagName('tbody').item(0);
  let rows = tableData.getElementsByTagName('tr');
  for(let i = 0; i < rows.length - 1; i++) {
    for(let j = 0; j < rows.length - (i + 1); j++) {
      if(getTdValue(rows, j, sortColumn) < getTdValue(rows, j+1, sortColumn)) {
        tableData.insertBefore(rows.item(j+1), rows.item(j));
      }
    }
  }
}
payne911 commented 3 years ago

For posterity: the bug was that the sorting was happening based on the very first digit of a column's numbers.