0

I have the following script which combines 2 feeds together:

$(document).Ready(function() {
    url = 'feed 1';
    url_2 = 'feed 2';

    $.when(
        $.ajax({
            type: "GET",
            url: document.location.protocol + '//ajax.googleapis.com/ajax/services/feed/load?v=1.0&num=1000&callback=?&q=' + encodeURIComponent(url),
            dataType: 'json'
        }),
        $.ajax({
            type: "GET",
            url: document.location.protocol + '//ajax.googleapis.com/ajax/services/feed/load?v=1.0&num=1000&callback=?&q=' + encodeURIComponent(url_2),
            dataType: 'json'
        })
    ).done(function(a1, a2) {
        var data = a1[0].responseData.feed.entries.concat(a2[0].responseData.feed.entries);
        if (data[0]) {
            for (i = 0; i <= data.length - 1; i++) {
                document.write(data[i].title);
                document.write("<br>");
                document.write(data[i].categories[0]);
                document.write("<br>");
                document.write(data[i].publishedDate);
                document.write("<br>");
                document.write(data[i].link);
                document.write("<hr>");
            }
        } 
        else {
            document.write("No records");
        }
    });
});

How do I go about sorting the joined feeds by publishedDate?

I think I'm supposed to use jquery.sort, but can't figure out how to use it with my current code.

1 Answer 1

1

You could do something like this

data.sort(function (a, b) {
    if (a.v > b.v) {
        return 1
    }
    if (a.v < b.v) {
        return -1
    }
    return 0;
});

Notes

  • change v to the variable name of the published data
  • data should be an array of the feeds
Sign up to request clarification or add additional context in comments.

4 Comments

This seems to be giving me strange results, i.e. 12 aug 2013, 16 sep 2013, 12 may 2013. I was expecting, 16 sep 2013, 12 aug 2013, 12 may 2013.
@oshirowanen change the return 1 to return -1 and vice versa
Still seems completely random, i.e. in the same month and year, I am getting 19, 22, 15 of september 2013, then 12 may 2013?
@oshirowanen can you create a jsfiddle, and then I will give it a bash there

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.