Question

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?

Was it helpful?

Solution

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());
});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top