3

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.

Machavity
  • 30,841
  • 27
  • 92
  • 100
Mircea
  • 11,373
  • 26
  • 64
  • 95
  • 2
    This method can probably be applied http://stackoverflow.com/questions/1134976/jquery-sort-list-items-alphabetically – cletus Apr 23 '10 at 11:13

2 Answers2

5
var arr = $('li').get();    
arr.reverse();

$.each(arr, function(i,v){
    $('ul').append(this);
});
jAndy
  • 231,737
  • 57
  • 305
  • 359
2

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
  • 3,608
  • 3
  • 28
  • 39
  • This will rearrange on alphabetical order. I'll try to modify it and let you know – Mircea Apr 23 '10 at 11:26
  • 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 Apr 23 '10 at 11:38
  • Sorry this one works only for text not IDs, I was unable to make it works with attr – Mircea Apr 23 '10 at 11:40