The following code snippet shows how to apply a grouping key:
After running these code lines, the collections array contains all the matched media items grouped and sorted according to album name.
To obtain a sorted list of songs, configure a media query with the MPMediaGroupingTitle key, or take advantage of the title key being the default for a media query. In either case, each obtained media item is, in effect, its own collection.
Collections sort according to the same rules used by iTunes on the desktop. This includes respecting the primary system language chosen by the user. Leading articles, including “A,” “An,” and “The” when using English, or “L’,” “La,” and “Le” when using French, are ignored during sorting. If you need precise control over sorting, implement it in your application.