Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 583
pixeltocode Avatar asked Jun 16 '10 11:06

pixeltocode


1 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'));
  });
});    
like image 155
jAndy Avatar answered Sep 28 '22 19:09

jAndy