Search code examples
javascriptspotifyspotify-app

Spotify is rendering playlists with tracks out of order


Currently, I am rendering a playlist full of tracks using the following code:

list = (List.forPlaylist(temp_playlist, {header:'no', fetch: 'once', maxItems:45, fields:['star','share','ordinal','track','time','popularity']}))
$('#playlists').appendChild(list.node)
list.init()

However, I find that the rendered playlist does not display the tracks in the same order in which they were added. It is important for me to retain the order in which they were added because I sort the tracks by popularity on the backend before adding them to the playlist. Is there a way to retain the order?


Solution

  • Turns out the problem was that I was adding the tracks one at a time to the collection of tracks instead of loading them all in an array and adding them simultaneously. According to the spotify docs for the Add method on the Collection model:

    "Note that it is better to call this method with multiple items than to call it multiple times with a single item, since the tracks are added asynchronously and could end up in the wrong order otherwise."