bla...">

Sort table rows according to array with jquery?

I have a table that looks something like this:

<table> <tr id="1"> <td>bla</td> </tr> <tr id="2"> <td>bla</td> </tr> <tr id="3"> <td>bla</td> </tr> <tr id="4"> <td>bla</td> </tr> </table> 

I also have an array according to which I have to sort the rows of this table, for example, the array looks something like this:

array = [3, 4, 1, 2];

Any hints or ideas, how can I reorder the lines with jquery / javascript to fit an array?

+4
source share
2 answers

Add them again in the order you need them. When you call .append and the item is already visible, it will be removed from its current location.

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

http://jsfiddle.net/nZ6HJ/

+4
source

jsBin demo

 for(i=0; i<array.length; i++){ $('#'+array[i]).appendTo('table'); } 
+2
source

All Articles