Sort date in rss from the latest

孤者浪人 提交于 2019-12-02 14:44:52

问题


How should I sort the date (latest at the top)?
Currently, the date is not sorted.

Below is my JSFiddle:

http://jsfiddle.net/qoLg6dnu/

$(document).ready(function() {

  $('#divRss').FeedEk({
    FeedUrl: 'https://www.google.com/finance/company_news?q=SGX:533&ei=EeiDWaGGMpXUuATxloPgAw&output=rss'
  });

  var r = Math.floor(Math.random() * 256);
  var g = Math.floor(Math.random() * 256);
  var b = Math.floor(Math.random() * 256);

  $('#example, .itemTitle a').css("color", getHex(r, g, b));

  $('#example').click(function() {
    $('.itemTitle a').css("color", getHex(r, g, b));
  });

  function intToHex(n) {
    n = n.toString(16);
    if (n.length < 2)
      n = "0" + n;
    return n;
  }

  function getHex(r, g, b) {
    return '#' + intToHex(r) + intToHex(g) + intToHex(b);
  }

});

回答1:


use Array.prototype.sort().

You import a FeedEk.js in your javaScript file to fetch data and render your dom elements for creating a list. You can sort data before rendering like this:

data.query.results.rss.sort(function(item1, item2) {
    var d1 = new Date(item1.channel.item.pubDate)
    var d2 = new Date(item2.channel.item.pubDate)
    return d2.getTime() - d1.getTime()
})

View all codes here



来源:https://stackoverflow.com/questions/45497903/sort-date-in-rss-from-the-latest

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!