Sort date in rss from the latest

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

});

Answers:

Answer

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

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us Javascript

©2020 All rights reserved.