如何在jQuery中对xml数据进行排序

pix*_*ode 6 xml sorting jquery

我怎样才能officers根据他们的所有内容进行排序ranks

jQuery的

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

XML(officer.xml)

<grade>
 <officer rank="2"></student>
 <officer rank="3"></student>
 <officer rank="1"></student>
</grade>
Run Code Online (Sandbox Code Playgroud)

谢谢.

jAn*_*ndy 8

$.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'));
  });
});    
Run Code Online (Sandbox Code Playgroud)