0

I'm trying to parse multiple subreddit feeds in a Google Script. I can call this Google Script (redditFeeds()) and it returns the title, link, and date to my spreadsheet. However, I want to sort the posts by date so I can see the most recent posts first. I've tried using sort() on the array in various ways and can't get anything sort by descending date. I've even tried converting the date to a Date object and that didn't fix it.

function redditFeeds() {

  var entries_array = [];
  var subreddit_array = ['https://www.reddit.com/r/funny/top/.rss','https://www.reddit.com/r/news/top/.rss']

  for (var s = 0; s < subreddit_array.length; s++) {

    var xml = UrlFetchApp.fetch(subreddit_array[s]).getContentText();
    var document = XmlService.parse(xml);
    var root = document.getRootElement();
    var atom = XmlService.getNamespace('http://www.w3.org/2005/Atom');
    var entries = document.getRootElement().getChildren('entry', atom);

    for (var i = 0; i < entries.length; i++) {
      var title = entries[i].getChild('title', atom).getText();
      var title = entries[i].getChild('link', atom).getText();
        var link = entries[i].getChild('link', atom).getAttribute('href').getValue();
      var date = entries[i].getChild('updated', atom).getValue();
      entries_array.push([title, link, date]);
    }
  }

  //return entries_array;

  //doesn't work
  //entries_array.sort(function(a,b) { 
  //  return a.date - b.date;
  //});

  //also not working
  return entries_array.sort(function(a,b) { 
    new Date(a.date).getTime() - new Date(b.date).getTime();
  });

}
Rubén
  • 34,714
  • 9
  • 70
  • 166
Keith
  • 673
  • 3
  • 13
  • 27
  • Possible duplicate of [Sorting an array of JavaScript objects](http://stackoverflow.com/questions/979256/sorting-an-array-of-javascript-objects) – Tibrogargan Jul 16 '16 at 21:21

3 Answers3

3

I think you want the below, assuming entries_array looks like I think it does. I have no idea what start was supposed to be in your code... I think each entry in entries_array is an array with three members in it, the third being some sort of representation of a date. If it's one that can be parsed by new Date, then this code should work:

return entries_array.sort(function (a, b) { 
  return new Date(a[2]) - new Date(b[2]);
});

If that's not right, please share what entries_array looks like.

user94559
  • 59,196
  • 6
  • 103
  • 103
1

I see a return missing, in the inner sort function and you should not need the getTime()

 return entries_array.sort(function(a,b) { 
    return new Date(a.start) - new Date(b.start);
  });
Hassan
  • 1,413
  • 1
  • 10
  • 12
  • 1
    This is true, but I believe both `a.start` and `b.start` are `undefined`, so this won't solve the issue. – user94559 Jul 16 '16 at 21:22
  • Definitely yes, but the OP question was related to sorting, I did not try to see if the data is valid or not – Hassan Jul 16 '16 at 21:23
0

An easy way of sorting date objects is by converting them into UNIX time stamps using dateObj.getTime(). This creates an integer of the seconds since midnight on New Years day 1970. It's very useful if you are working in multiple time zones.

grateful
  • 1,128
  • 13
  • 25