我如何根据其officers对所有ranks进行排序

jQuery的

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

XML(legs.xml)
<grade>
 <officer rank="2"></student>
 <officer rank="3"></student>
 <officer rank="1"></student>
</grade>

谢谢。

最佳答案

$.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'));
  });
});

09-07 07:53