Eric Schoonover Eric Schoonover - 4 months ago 40
jQuery Question

What is the easiest way to order a <UL>/<OL> in jQuery?

I'm looking for some sample code that will sort the list items in an HTML list by alphabetical order. Can anyone help?

Here is a sample list for people to work with:

<ul class="alphaList">

var items = $('.alphaList li').get();
  var keyA = $(a).text();
  var keyB = $(b).text();

  if (keyA < keyB) return -1;
  if (keyA > keyB) return 1;
  return 0;
var ul = $('.alphaList');
$.each(items, function(i, li){