Using jQuery to Sort a List
August 25, 2009
I needed to add a new feature today, and wanted an easy client-side solution. Since I use jQuery in the project already, I searched and found a good starting point in a blog post by Bill Richards. The code I found adds a function and a sort handler that can be applied to a list. jQuery.fn.sort = function() { return this.pushStack( [].sort.apply( this, arguments ), []); }; function sortAlpha(a,b){ return a.innerHTML > b.innerHTML ? 1 : -1; }; Here is a simple example of the HTML I am working with: <ul id="caseListing"> <li> <div id="1"> <a href="/Cases/Show/1">Case 1</a>: need help with VPN connection </div> </li> <li> <div id="3"> <a href="/Cases/Show/3">Case 3</a>: blue screen of death upon login </div> </li> <li> <div id="6"> <a href="/Cases/Show/6">Case 6</a>: Computer has no power </div> </li> </ul> …