如何在JQuery中按日期对xml数据进行排序

时间:2013-11-16 23:19:31

标签: javascript jquery xml sorting date

此应用程序从RSS提要中提取信息,我需要按日期对文章进行排序。 xml数据包含一个'Date'元素,该元素以ddmmyy格式列出日期。我的问题:我是否可以在jQuery中实现一个排序函数来对信息进行排序并以正确的日期顺序显示它? (日期顺序应该是显示的第一个条目是最远的日期,例如:01/20/14然后01/07/2014然后12/22/13等... 以下是我到目前为止的情况:

<script type="text/javascript">
 $(document).ready(function() {

        $('input[type=radio]').click(function() {
            var id = this.id;
            if(id == 'radio-bio') { var categoryURL = '/BayAreaTech/wp-rss2.php?cat=15';}
            else if (id == 'radio-com'){ var categoryURL = '/BayAreaTech/wp-rss2.php?cat=13';}
            else if (id == 'radio-eleP'){ var categoryURL = '/BayAreaTech/wp-rss2.php?cat=9';}
            else if (id == 'radio-eleD'){ var categoryURL = '/BayAreaTech/wp-rss2.php?cat=10';}
            else if (id == 'radio-nano'){ var categoryURL = '/BayAreaTech/wp-rss2.php?cat=16';}
            else if (id == 'radio-opt'){ var categoryURL = '/BayAreaTech/wp-rss2.php?cat=12';}
            else if (id == 'radio-semi'){ var categoryURL = '/BayAreaTech/wp-rss2.php?cat=11';}
            else { var categoryURL = '/BayAreaTech/wp-rss2.php?cat=1';}

            $('#feedContainer').empty();
                        $.ajax({
                        type: 'GET',
                        url: categoryURL,
                        dataType: 'xml',
                        success: function (xml) {
                            $(xml).find("item").each(function () {
                                  var title = $(this).find("title").text();
                                  var date = $(this).find("Date").text();
                                  var region = date.substr(6);
                                        if (region.length < 3) { region = "ALL"; }  
                                  var description = $(this).find("description").text();
                                  var descriptdisplay = description.substr(0, description.indexOf(",")+6); //Parsed DATE from description
                                        if (descriptdisplay.length > 35) { descriptdisplay = "See event for details"; }
                                    //var locationdisplay = description.substr(description.indexOf(",")+6,4); //Parsed the location from description
                                  var category = $(this).find("category").text();
                                  var linkUrl = $(this).find("link").text();
                                  var displaytitle = "<a href='" + linkUrl + "' target='_blank'>" + title + "</a>"  
                                  $('#feedContainer').append('<h3>'+displaytitle+'</h3><p>'+"Event Date: "+descriptdisplay+'</p><p>'+"Location: "+region+'</p');

                            });
                        }
                    });

        });
 });
 </script>

1 个答案:

答案 0 :(得分:2)

建议您首先将xml解析为可以排序的对象数组,然后再解析为html

success: function (xml) {
    var data=[];
    $(xml).find("item").each(function () {
      var dateText= $(this).find("Date").text()

      var item={
           title: $(this).find("title").text(),
           dateText =dateText,
           date : new Date( dateText),
           /* other properties*/
        }  
        /* push object to array*/
        data.push( item);

    });

    /* sort data*/
    data.sort(function(a,b){
         return a.date > b.date;
    });

     /* now parse to html */    
      $.each(data, function(index, item){


      })