I\'m trying to sort a list of divs with jQuery. Essentially the list might look like this:
I wrote a small plugin just for this purpose. Feel free to steal. Basically you select elements, sort them, and reappend in the new order.
==============================================================================
Per Jason's request including code here
$(".contest_entry").orderBy(function() {return +$(this).text();}).appendTo("#parent_div");
#parent_div
is a container for the .contest_entry
s.
The +
is just a sneaky way to convert value to number to force number compare rather than string compare (unless that is what you want).
orderBy()
is a sorting plugin that I wrote. I expanded on it quiet a bit since then, but here is the simple version:
jQuery.fn.orderBy = function(keySelector)
{
return this.sort(function(a,b)
{
a = keySelector.apply(a);
b = keySelector.apply(b);
if (a > b)
return 1;
if (a < b)
return -1;
return 0;
});
};