A while back I posted a simple sorting algorithm for number sorting, but it doesn't really do the trick for regular strings (e.g. anchors). But since I needed one recently I wrote another small and very simple sorting solution.
For a working example, see: http://dynom.nl/jquery/listsort.php
My.List = {
Sort : function (listSelector) {
var sortedElements, left, right;
// If we have any list items to sort on
if ($(listSelector + ' li').length > 0) {
// Remove the items from the DOM
sortedElements = $(listSelector + ' li').remove();
// Sort, using a custom sorting and compare by anchor contents
sortedElements.sort(function (a, b) {
left = $(a).children('a').html();
right = $(b).children('a').html();
if (left > right) {
return 1;
} else if (right < left) {
return -1;
} else {
return 0;
}
});
// Place the items back on the DOM
$(listSelector).append(sortedElements);
}
}
};
Again, the source should be self explanatory