1

I want to sort the artists query by album like the Music app. How can I do that?

var artistsQuery = MPMediaQuery.artistsQuery()
var artistsQuery.groupingType = MPMediaGrouping.AlbumArtist
var songsByArtist = artistsQuery.collections
evenwerk
  • 947
  • 1
  • 12
  • 28

2 Answers2

1

There is actually a shorter way of doing this. You could assign a grouping type to the query

artistQuery = MPMediaQuery.artistsQuery()
artistQuery.groupingType = MPMediaGrouping.AlbumArtist
artists = artistQuery.collections

that's how I'm doing it at least

NoSixties
  • 2,443
  • 2
  • 28
  • 65
-1

This code will create a mutable array. The artistsItemsSortedByAlbum.count equals the amount of artists & the artistsItemsSortedByAlbum[index].count equals the amount of albums.

         var artistsItemsSortedByAlbum = NSMutableArray()

         for var i = 0; i < artists.count; i++ {
            let collection:MPMediaItemCollection = artists[i] as! MPMediaItemCollection
            let rowItem = collection.representativeItem!

            let albumsQuery = MPMediaQuery.albumsQuery()
            let albumPredicate:MPMediaPropertyPredicate = MPMediaPropertyPredicate(value: rowItem.valueForProperty((MPMediaItemPropertyAlbumArtist)), forProperty: MPMediaItemPropertyAlbumArtist)
            albumsQuery.addFilterPredicate(albumPredicate)

            let artistAlbums = albumsQuery.collections
            artistsItemsSortedByAlbum.addObject(artistAlbums!)
        }
evenwerk
  • 947
  • 1
  • 12
  • 28