Eric Schoonover Eric Schoonover - 1 year ago 80
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">

Answer Source
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){
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download