I ended up finding a code sample from this question. I'm working with a lot fewer than 1,200 items, so either the code in the question or the answer would work for me, but I used the one in the answer for efficiency. (It also uses MPMediaItemPropertyAlbumPersistentID
in addition to the album title, which I like as a check against albums/shows with the same title.)
sortedArray = [arrayMediaItems sortedArrayUsingComparator:^NSComparisonResult(id a, id b) {
NSComparisonResult compareResult;
NSString *first1 = [(MPMediaItem*)a valueForProperty:MPMediaItemPropertyAlbumTitle];
if(first1 == nil) first1 = @" "; // critical because compare will match nil to anything and result in NSOrderedSame
NSString *second1 = [(MPMediaItem*)b valueForProperty:MPMediaItemPropertyAlbumTitle];
if(second1 == nil) second1 = @" "; // critical because compare will match nil to anything and result in NSOrderedSame
compareResult = [first1 compare:second1];
if (compareResult == NSOrderedSame) {
NSString *first2 = [(MPMediaItem*)a valueForProperty:MPMediaItemPropertyAlbumPersistentID];
NSString *second2 = [(MPMediaItem*)b valueForProperty:MPMediaItemPropertyAlbumPersistentID];
compareResult = [first2 compare:second2];
if(compareResult == NSOrderedSame) {
NSString *first3 = [(MPMediaItem*)a valueForProperty:MPMediaItemPropertyAlbumTrackNumber];
NSString *second3 = [(MPMediaItem*)b valueForProperty:MPMediaItemPropertyAlbumTrackNumber];
compareResult = [first3 compare:second3];
}
}
return compareResult;
}];
I hope this helps someone!