-4

i'm trying to inital sort a table by it's row-numbers from an array/variable like

$list: row0=id2;row1=id0;row2=id1...

<table>
<tr id="0">...</tr>
<tr id="1">...</tr>
<tr id="2">...</tr>
</table>

How can i handle with tablesorter that the rows will be sorted like in $list?

thanks for any tipp or workaround :)

vaso123
  • 12,347
  • 4
  • 34
  • 64
John Doe
  • 45
  • 1
  • 5

2 Answers2

0

This is a possible duplicate of: How to sort DOM elements while selecting in jQuery?

$('#myt tr').sort(function(a, b) {
  if (parseInt(a.id) > parseInt(b.id)) return 1;
  else return -1;
}).each(function() {
  $('#myt').append($(this));
});
td {
  border: 1px solid grey;
  padding: 10px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<table id='myt'>
  <tr id="8">
    <td>8</td>
  </tr>
  <tr id="1">
    <td>1</td>
  </tr>
  <tr id="21">
    <td>21</td>
  </tr>
  <tr id="5">
    <td>5</td>
  </tr>
</table>
Community
  • 1
  • 1
Ahmad
  • 12,336
  • 6
  • 48
  • 88
-1

maybe not what was originally intended but it's a workaround :)

var sortTable = [1,5,2,3,4];
$.each(sortTable, function(){
   $("table#productList").append($("#" + this));
 })

thanks for the responses and help!!

John Doe
  • 45
  • 1
  • 5