4

Possible Duplicate:
jQuery: Sort div's according to content of different sub divs

Is it possible to sort the contents of div

<div id="sort">
    <div id="something">A2</div>
    <div id="something">A1</div>
    <div id="something">A4</div>
    <div id="something">A3</div>
</div>

sorted to:

<div id="sort">
    <div id="something">A1</div>
    <div id="something">A2</div>
    <div id="something">A3</div>
    <div id="something">A4</div>
</div>

Anything like this????

$('#sort').sort();
Community
  • 1
  • 1
Shiv
  • 1,211
  • 4
  • 14
  • 24

1 Answers1

19

What about this?

var mylist = $('#sort');

var listitems = mylist.children('div').get();

listitems.sort(function(a, b) {
   return $(a).text().toUpperCase().localeCompare($(b).text().toUpperCase());
});

$.each(listitems, function(index, item) {
   mylist.append(item); 
});

Pulled from: http://www.onemoretake.com/2009/02/25/sorting-elements-with-jquery/

And This: How may I sort a list alphabetically using jQuery?

Community
  • 1
  • 1
Etienne Dupuis
  • 13,548
  • 6
  • 47
  • 58