Sorting with jQuery


Source Code:

<script>
	$(document).ready(function(){
		var mylist = $("ul");
		var listitems = mylist.children("li").get();
		listitems.sort(function(a, b) {
			var compA = $(a).text().toUpperCase();
			var compB = $(b).text().toUpperCase();
			return (compA < compB) ? -1 : (compA > compB) ? 1 : 0;
		})
		$.each(listitems, function(idx, itm) {
			mylist.append(itm);
		});
	});
</script>

Buy us coffee

Reviews and Comments