I have a HTML table like this:
<table>
<tr><td>X</td><td>X</td><td class='latest order1'>X</td><td class='latest order4'>X</td></tr>
<tr><td>X</td><td>X</td><td class='latest order3'>X</td><td class='latest order2'>X</td></tr>
<tr><td>X</td><td>X</td><td class='latest order6'>X</td><td class='latest order5'>X</td></tr>
</table>
Now, I want to run a jQuery action (an ajax call) on all objects of latest class, that’s a simple:
$('.latest').each(do_call());
But since the ajax action takes some time, I have the elements ordered by their importance. And I would like to run the do_call() for object with order1, then for order2 element, and so on.
How can I sort jQuery objects, so the actions would run in proper order?
First, get all the
.latestelements and their order classes in an array:Then sort the array and loop through it, get the corresponding element:
This method works even if your
.latestelements are not sequentially indexed; eg. you can haveorder4afterorder1with noorder2ororder3present and it still works.