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.
I spent some time figuring out a way to sort unordened lists, when dynamically (AJAX) adding/removing items. I had some problems with the typical '10 being greater then 1, but lesser then 2', so I took a simple bubblesort algorithm and tweaked it with the string length. It's probably not the fastest or most elegant way of doing it, but it works like a charm. If you have any improvement points, don't be shy and drop a line.
This is the search algorithm I used, (which I found here) and I simply added the difference in string length to the equation to make sure that '10' is greater then '1', causing human-natural sorting.
// Search algorithm, if lesser then 0 it's smaller then the current item. // If it's greater then 0, it's "larger" then the current item. cmpr = ( (newItem > curListItem) - (curListItem > newItem) + (newItem.length - curListItem.length) );
Before: Title 1, Title 10, Title 13, Title 2, Title 3, Title 4 After: Title 1, Title 2, Title 3, Title 4, Title 10, Title 13