views:

210

answers:

2

Hi, I have the following code:

<ul>
<li id="project_25">Proj Something</li>
<li id="project_26">Proj Blah</li>
<li id="project_27">Proj Else</li>
<li id="project_31">Proj More</li>
...
</ul>

Is there a way to arrange these LIs, reverse their order, based on the LI ID?

Something like:

<ul>
<li id="project_31">Proj More</li>
<li id="project_27">Proj Else</li>
<li id="project_26">Proj Blah</li>
<li id="project_25">Proj Something</li>
...
</ul>

Thank you.

+1  A: 

Please try the code given below:

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); });

I found this here.

HTH

Raja
This will rearrange on alphabetical order. I'll try to modify it and let you know
Mircea
That was just a sample to give you an idea. Instead of getting text() try to get the number (by splitting it) and then compare it. HTH
Raja
Sorry this one works only for text not IDs, I was unable to make it works with attr
Mircea
+3  A: 
    var arr = $('li').get();    
    arr.reverse();

    $.each(arr, function(i,v){
      $('ul').append(this);
    });

Kind Regards

--Andy

jAndy
Thanx Andy, this works!
Mircea