Floris Verloop
Floris Verloop

Reputation: 3

Can I specify sorting for results from the Spotify Apps API?

I'm a novice Javascript programmer, so I apologize if this question makes no sense.

When I do a request for all the albums in a library, I get an array for every album. Is it possible to get these arrays sorted by artist name?

Upvotes: 0

Views: 491

Answers (1)

Ivan Navarrete
Ivan Navarrete

Reputation: 535

You can sort the array of albums yourself. This solution will place albums with no artist name, first.

var sp = getSpotifyApi(1);
var m = sp.require('sp://import/scripts/api/models')

var sortedAlbums = m.library.albums.sort(function(album1, album2) {
  var name1 = album1.artist.name || "";
  var name2 = album2.artist.name || "";

  return name1.toLocaleLowerCase().localeCompare(name2.toLocaleLowerCase());
});

Upvotes: 2

Related Questions