Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery - Sorting div contents [duplicate]

Tags:

jquery

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();
like image 496
Shiv Avatar asked Nov 30 '22 23:11

Shiv


1 Answers

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?

like image 124
Etienne Dupuis Avatar answered Jan 03 '23 01:01

Etienne Dupuis