I've been playing around trying to get a function that will sort a selection of li tags by their content but currently to no avail (at least no speed/accuracy);
$('.sortasc').live('click',function(){
var liArr = Array();
$('#licontainer').children('li').each(function(){
liArr.push($(this).html());
});
liArr.sort(alphaNumSort);
$(liArr).each(function(){
var current = this;
var clone = $('li').filter(function(){return($(this).html()==current);}).clone();
$('li').filter(function(){return($(this).html()==current);}).remove();
clone.appendTo('#tempsortbox');
});
$('#licontainer').html($('#tempsortbox').html());
$('#tempsortbox').html('')
});
It's both slow and not GREAT at sorting. Idealy, it would sort based on the content of a strong tag that resides within the li.
Here's the alphaNumSort function if you're interested (this works a treat its just the crappy html and cloning rubbish that's not really working)
function alphaNumSort(m,n){
try{
var cnt= 0,tem;
var a= m.toLowerCase();
var b= n.toLowerCase();
if(a== b) return 0;
var x=/^(\.)?\d/;
var L= Math.min(a.length,b.length)+ 1;
while(cnt< L && a.charAt(cnt)=== b.charAt(cnt) &&
x.test(b.substring(cnt))== false && x.test(a.substring(cnt))== false) cnt++;
a= a.substring(cnt);
b= b.substring(cnt);
if(x.test(a) || x.test(b)){
if(x.test(a)== false)return (a)? 1: -1;
else if(x.test(b)== false)return (b)? -1: 1;
else{
var tem= parseFloat(a)-parseFloat(b);
if(tem!= 0) return tem;
else tem= a.search(/[^\.\d]/);
if(tem== -1) tem= b.search(/[^\.\d]/);
a= a.substring(tem);
b= b.substring(tem);
}
}
if(a== b) return 0;
else return (a >b)? 1: -1;
}
catch(er){
return 0;
}
}
Cheers