Basically, use jQuery to get the parent container and a list of the things you want to sort. Then, call sort() with an anonymous function that sorts how you want. Finally, iterate the list, appending the items to the parent container. When you append something that already exists, it just moves the item in the DOM, which is exactly what we want. I can call this code each time I add a new span tag, and the list stays sorted...nice.
// sort span tags within a td
var $td = $('#tdMyItems');
var itms = $td.children('span').get();
itms.sort(function (a, b)
{
var compA = $(a)[0].id;
var compB = $(b)[0].id;
return (compA > compB) ? -1 : (compA < compB) ? 1 : 0;
})
$.each(itms, function (idx, itm)
{
$td.append(itm);
});
