Horst Knacklgrubr
Horst Knacklgrubr

Reputation: 13

Youtube API array sorting with JQuery

I'm having some trouble sorting the results in this code. The results order is just random every time... How can i fix this?

And I want to make a function for the repeating GET code with an array of the channel ID's. Who can help me please? It's much appreciated.

var ItemArray = [];

var d1 = $.get("https://www.googleapis.com/youtube/v3/search?channelId=UCVQ2Z9dNQ2aJJ10f6SgBH0g&type=video&order=date&maxResults=1&part=snippet&KEY",
        function (data) {
            $.each(data.items, function (i, item) {
idee = item.id.videoId;
tittie = item.snippet.title;
cattit = item.snippet.channelTitle;
datie = item.snippet.publishedAt;
ItemArray.push([datie, cattit, idee, tittie]);
            });
        });

var d2 = $.get("https://www.googleapis.com/youtube/v3/search?channelId=UC2xskkQVFEpLcGFnNSLQY0A&type=video&order=date&maxResults=1&part=snippet&KEY",
        function (data) {
            $.each(data.items, function (i, item) {
idee = item.id.videoId;
tittie = item.snippet.title;
cattit = item.snippet.channelTitle;
datie = item.snippet.publishedAt;
ItemArray.push([datie, cattit, idee, tittie]);
            });
        });

var d3 = $.get("https://www.googleapis.com/youtube/v3/search?channelId=UCGHi_s4RrqUh4hsS4mLbiPg&type=video&order=date&maxResults=1&part=snippet&key=KEY",
        function (data) {
            $.each(data.items, function (i, item) {
idee = item.id.videoId;
tittie = item.snippet.title;
cattit = item.snippet.channelTitle;
datie = item.snippet.publishedAt;
ItemArray.push([datie, cattit, idee, tittie]);
            });
        });

var d4 = $.get("https://www.googleapis.com/youtube/v3/search?channelId=UCy5QKpDQC-H3z82Bw6EVFfg&type=video&order=date&maxResults=1&part=snippet&key=KEY",
        function (data) {
            $.each(data.items, function (i, item) {
idee = item.id.videoId;
tittie = item.snippet.title;
cattit = item.snippet.channelTitle;
datie = item.snippet.publishedAt;
ItemArray.push([datie, cattit, idee, tittie]);
            });
        });
$.when(d1, d2, d3, d4).done(function() {

ItemArray.sort(function(a, b) {
        return a[0] - b[0];
});

for(i=0;i<=ItemArray.length;i++){
$('#mytable').append('<tr><td>'+ItemArray[i][0]+'</td><td><a target="_blank" href="https://www.youtube.com/user/'+ItemArray[i][1]+'">'+ItemArray[i][1]+'</a></td><td><a target="_blank" href="https://www.youtube.com/watch?v='+ItemArray[i][2]+'">'+ItemArray[i][3]+'</a></td></tr>');
}
})
})

Upvotes: 0

Views: 147

Answers (1)

johnnycrab
johnnycrab

Reputation: 997

Accoding to the YouTube Data API documentation, publishedAt is being returned specified in ISO 8601 (YYYY-MM-DDThh:mm:ss.sZ) format.

As Array.prototype.sort requires you to return a positive/negative number, I would compare the number of milliseconds, i.e. store your date like

datie = new Date(item.snippet.publishedAt)

and comparing it in your sort-function as

return a[0].getTime() - b[0].getTime()

Upvotes: 2

Related Questions