Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery selector - how to sort the iterated objects

Let say there are 3 divs Div1, Div2, Div3 and all have the class "ui-selected"

To iterate the div selection you can use something like below

$(".ui-selected").each(...)

The above iteration, references each div in the same sequence as they are added to the Document.

How do we sort the selection.

For e.g if Div1 represents a value of 30, Div2 represents value of 10 and Div3 a value of 40

the iteration when sorted should be as

Div2 , Div1 and Div3.

Right now the list iterates in the order of how they belong in the Doc model.

Is there a way to sort the jquery selection?

like image 235
Nilesh Avatar asked Mar 05 '26 16:03

Nilesh


1 Answers

Assuming you have a way to know the "value" of each div, you can use the sort method.

$(".ui-selected").sort(function(a,b){
    //sorting logic here
    }).each(...);
like image 159
Becuzz Avatar answered Mar 08 '26 19:03

Becuzz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!