Some data from the API does not appear in the correct order - Tumblr and Last.fm

I have a list of my recent Tumblr audio messages that are part of the Tumblr API, and with each message is a summary of the artist from Last.fm API. I have data arriving correctly, but audio messages are not displayed in chronological order. Each time I refresh the page, they appear randomly. I would like the latest audio messages to be displayed at the top. Here is my code:

    $(document).ready(function() {
        var user = "nycxnc";
        var key =  "###";
        var type = "audio";
        var limit = "5";
        var url = "https://api.tumblr.com/v2/blog/" + user +
            ".tumblr.com/posts/" + type + "?callback=?&filter=" + type +
            "&limit=" + limit + "" + key + "";
        $.getJSON(url, function(data) {
            $.each(data.response.posts, function(i, item) {
                var artist = item.artist;
                var track = item.track_name;
                var url2 =
                    "http://ws.audioscrobbler.com/2.0/?method=artist.getInfo&track=" +
                    track + "&artist=" + artist +
                    "&api_key=###&autocorrect=1&format=json&callback=?";
                $.getJSON(url2, function(data) {
                    $.each(data, function(i, item) {
                        artist_summary = item.bio.summary;
                        artist_image = item.image[4]["#text"];
                        $(".au-wrapper").append(
                            '<div class="audio-wrapper"><img src=' + artist_image +
                            '><div class="audio-meta"><span class="audio-title">' +
                            track + '</span><div class="audio-artist">' + artist +
                            '</div></div><div class="url">' + artist_summary +
                            '</div></div>');
                    });
                });
            });
        });
    });

How can I combine the data from the two APIs in the correct order if the second JSON URL (Last.fm) depends on the data from the Tumblr API? I am new to Javascript and I am creating this script as a training experiment, so any help would be greatly appreciated. (: Thank.

+4

All Articles