I have three div
s and I want to sort them by height, from largest to smallest.
<div>smallest</div>
<div>largest</div>
<div>middle</div>
Any idea?
It's quite simple. Use .sort()
:
$('div').sort(function (a, b) {
return $(a).height() > $(b).height() ? 1 : -1;
}).appendTo('body');
Code: http://jsfiddle.net/fEdFt/2/
Since jQuery returns an array, you can use the sort
method on array to reorder the elements. Once they are sorted in the proper order, you can then remove them from the DOM and reinsert them in the order desired.
$('div.sortable').sort( function(a,b) {
return $(b).height() - $(a).height();
}).appendTo('#container');
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With