How to sort XML data in jQuery

how can i sort all officers based on their ranks

JQuery

 $.get('officers.xml', function(grade){ $(grade).find('officer').each(function(){ var $rank = $(this).attr('rank'); }); }); 

XML (officer.xml)

 <grade> <officer rank="2"></student> <officer rank="3"></student> <officer rank="1"></student> </grade> 

thanks.

+6
jquery sorting xml
source share
3 answers
 $.get('officers.xml', function(grade){ var officer = $(grade).find('officer'); officer.sort(function(a, b){ return (parseInt($(a).attr('rank')) - parseInt($(b).attr('rank'))); }); officer.each(function(i,v){ alert($(v).attr('rank')); }); }); 
+8
source share

If you dynamically generate your XML file on the server, the best way is to sort the data on the server side. Some discussion here .

+2
source share

Something like this should work

 var officers = $('officer'); // unsorted function matchRank(a, b) { return (int)a.attr('rank') - (int)b.attr('rank'); }; officers.sort(matchRank); // sorted 
0
source share

All Articles