
JQuery. Write less, do more
UPDATE:
As Tom point out in the comments, beginning with jQuery 1.3.2 this can be further simplified to the following.
function sortAlpha(a,b){ return a.innerHTML.toLowerCase() > b.innerHTML.toLowerCase() ? 1 : -1; }; $('ol li').sort(sortAlpha).appendTo('ol');
Old code:
jQuery.fn.sort = function() { return this.pushStack( [].sort.apply( this, arguments ), []); }; function sortAlpha(a,b){ return a.innerHTML > b.innerHTML ? 1 : -1; }; $('ol li').sort(sortAlpha).appendTo('ol');
Short and sweet. The result is a in place sort based off the innerHTML alphabetically.