Javascript table sorting works well, but iterating over all indexes gives different results

I have a table that I sort using the following function:

function ReorderSupplyGP(table){
  table.find('tr:not(.kn-table_summary)').sort(function (a, b) {
    var tda = $(a).find('td:eq(1)').text().trim();
    var tdb = $(b).find('td:eq(1)').text().trim();

    var sales = {
       "Purchase": 0, "Transfer": 1, "Returns": 2, "Adjustment": 3, "Inventory": 4
    };

    tda = sales[tda];
    tdb = sales[tdb]; 

    // if a < b return 1
    return tda > tdb ? 1
    // else if a > b return -1
    : tda < tdb ? -1
    // else they are equal - return 0    
    : 0;
  }).prependTo(table);


}

It works fine, but when I print all the lines using a function:

for(x=1;x<tablebodyrow.length;x++)
  {
    console.log(tablebodyrow.eq(x));
  }

It seems that the rows are sorted visually, but their index never changed. Is there any way to solve this problem.

+4
source share
1 answer

this is exactly what you are doing:

1) you fill some html table with values ​​from some collection X 2), then you get the html table, read the values ​​from your rows and reorder the rows of the html table 3) you do not reorder the base collection anywhere

: 1) 2) , html

+2

All Articles