0

I have some code to sort the body of a table that is all numbers. It works on firefox, but on chrome it does not update the table. On chrome, the sort function does not seem to change the order of the tr elements in the array.

function sortTable(col, dir = 1) {

    var tb = document.getElementById("vote_table").tBodies[0];
    tr = Array.prototype.slice.call(tb.rows, 0);

    tr = tr.sort(
            function (a, b) 
            { 
                return dir*parseInt(a.cells[col].textContent) < dir*parseInt(b.cells[col].textContent);               
            }
       );
    
    alert(tr[0].cells[col].textContent);

    for(var i = 0; i < tr.length; ++i) 
    { 
        tb.appendChild(tr[i]);  
    } // append each row in order
}
user2288107
  • 59
  • 1
  • 6
  • Relevant for sorting: [Sorting in JavaScript: Shouldn't returning a boolean be enough for a comparison function?](https://stackoverflow.com/q/24080785) Might even be the actual issue you have with your code. – VLAZ Mar 08 '22 at 17:32
  • If you can use default values for function parameters (`dir = 1`) then you can also use `Array.from()`, spread syntax (`...`) and `.append()` – Andreas Mar 08 '22 at 17:34
  • 1
    Please add an actual [mcve] (-> a table with just enough rows to reproduce the problem) – Andreas Mar 08 '22 at 17:36
  • @VLAZ, it was the sorting thing thanks!! – user2288107 Mar 08 '22 at 17:43

0 Answers0