如何在javascript中对已解析的内容进行排序

时间:2015-12-05 10:58:06

标签: javascript html sorting rss

我已经从不同的来源解析了rss-feeds,我想按日期对它们进行排序。但排序功能不起作用。它随机对“newsdiv”块进行排序。我不明白为什么。 如果我将内容分类直接写入html文件,那么该函数效果很好。

这是代码:

function getRSS(feedUrl) {

$.get('proxy.php?url=' + feedUrl, function(data) {
    $(data).find('item').each(function() {

        var title = $(this).find('title').text();
        var url = $(this).find('link').text();
        var description = $(this).find('description').text();
        var pubDate = $(this).find('pubDate').text();

        var date = new Date(pubDate);
        var months = Array("01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12");
        var string = date.getDate() + "-" + months[date.getMonth()] + "-" + date.getFullYear();

        var enc = $(this).find('enclosure');

        image_url = $(enc).attr('url');

        var html;
        html  = "<div class=\"newsdiv\"><div class=\"rsstitle\"><a href=\"" + url + "\" target=\"_blank\">"+title+"</a></div>";
        html += "<div class=\"rssdate\">" + string + "</div>";
        html += "<div class=\"rssdescription\">" + description + "<a href=\"" + url + "\" target=\"_blank\"> more</a></div>";
        html += "<img class=\"media\" src=\"" + image_url + "\" onerror=\"this.src='images/p.gif'\"></div>";

        $('#rssContent').append($(html));
    });
});
}

function getRSS1(feedUrl) {

$.get('proxy.php?url=' + feedUrl, function(data) {
    $(data).find('item').each(function() {

        var title = $(this).find('title').text();
        var url = $(this).find('link').text();
        var description = $(this).find('description').text();
        var pubDate = $(this).find('pubDate').text();

        var date = new Date(pubDate);
        var months = Array("01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12");
        var string = date.getDate() + "-" + months[date.getMonth()] + "-" + date.getFullYear();

        var enc = $(this).find('enclosure');

        image_url = $(enc).attr('url');

        var html;
        html  = "<div class=\"newsdiv\"><div class=\"rsstitle\"><a href=\"" + url + "\" target=\"_blank\">"+title+"</a></div>";
        html += "<div class=\"rssdate\">" + string + "</div>";
        html += "<div class=\"rssdescription\">" + description + "<a href=\"" + url + "\" target=\"_blank\"> more</a></div>";
        html += "<img class=\"media\" src=\"" + image_url + "\" onerror=\"this.src='images/p.gif'\"></div>";

        $('#rssContent').append($(html));
    });
});
}

function getRSS2(feedUrl) {

$.get('proxy.php?url=' + feedUrl, function(data) {
    $(data).find('item').each(function() {

        var title = $(this).find('title').text();
        var url = $(this).find('link').text();
        var description = $(this).find('description').text();
        var pubDate = $(this).find('pubDate').text();

        var date = new Date(pubDate);
        var months = Array("01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12");
        var string = date.getDate() + "-" + months[date.getMonth()] + "-" + date.getFullYear();

        var html;
        html  = "<div class=\"newsdiv\"><div class=\"rsstitle\"><a href=\"" + url + "\" target=\"_blank\">"+title+"</a></div>";
        html += "<div class=\"rssdate\">" + string + "</div>";
        html += "<div class=\"rssdescription\">" + description + "</div></div>";

        $('#rssContent').append($(html));
    });
});
}

$(document).ready(function() {
getRSS("http://source1.com");
getRSS1("http://source2.com");
getRSS2("http://source3.com");
});

function sortDescending(a, b) {
var date1 = $(a).find(".rssdate").text();
date1 = date1.split('-');
date1 = new Date(date1[2], date1[1] - 1, date1[0]);
var date2 = $(b).find(".rssdate").text();
date2 = date2.split('-');
date2 = new Date(date2[2], date2[1] - 1, date2[0]);

return date1 < date2;
};

function sortNews() {
$('#rssContent .newsdiv').sort(sortDescending).appendTo('#rssContent');
};

1 个答案:

答案 0 :(得分:0)

我弄清楚了什么是错的。

return date2 - date1;