Possible Duplicate:
Sort element by numerical value of data attribute
I want to reorganize divs according to a data attribute set on them, ranging from highest integer to lowest. Right now, I push all the data into an array and sort that but I'm not sure how to proceed. How can I use this to reorganize divs inside their parent div?
var prices = new Array();
$(this).children('.listing-item').each(function() {
var price = parseInt($(this).attr('data-listing-price'));
prices.push(price)
});
prices.sort(function(a,b){return b-a});
This gets me an array like 139,129,129,109,109,84 for example. Now my idea was to loop through the divs another time with a selector like this:
$('.listing-item[data-listing-price="' + prices[0] + '"]')
but I'm not sure how to move the divs (the div with the highest data-attribute integer should move to the top and so on. Any idea?