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

时间:2022-11-26 01:35:31

how can i sort all officers based on their ranks

我如何根据他们的队伍对所有军官进行排序

jQuery

jQuery的

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

XML (officer.xml)

XML(officer.xml)

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

thanks.

谢谢。

3 个解决方案

#1


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

#2


2  

In case you generate dynamically your xml file on the server the best way is to sort data on the server side. Some discussion is here.

如果您在服务器上动态生成xml文件,最好的方法是在服务器端对数据进行排序。一些讨论在这里。

#3


0  

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

#1


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

#2


2  

In case you generate dynamically your xml file on the server the best way is to sort data on the server side. Some discussion is here.

如果您在服务器上动态生成xml文件,最好的方法是在服务器端对数据进行排序。一些讨论在这里。

#3


0  

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