2

iTunes has a way of sorting the songs that the ones with title starts with "a" or "the" are recognized without it. Is there a way to access this "sort title"? I am pretty sure Apple Music does this and I guess it can be accessed by

let item = MPMediaItem()
let sortName = item.value(forProperty: "someMagicSortNamePropertyIdentifier") as! String

I am talking about this fields in iTunes enter image description here

Adam
  • 1,776
  • 1
  • 17
  • 28

1 Answers1

2

Basic format for all sort Properties is sortProperty i.e. sortTitle, sortAlbumTitle, sortAlbumArtist etc.

func sortProperty(for property: String) -> String{
    return "sort" + property.prefix(1).uppercased() + property.dropFirst()
}
Casper
  • 152
  • 12
  • 1
    It works. You can't imagine how helpful this is, thank you so much! – Adam Jan 15 '19 at 09:59
  • 1
    One thing to note is the property would have `nil` if the value is not set, so fallback to actual property if the value is `nil`. – Casper Jan 20 '19 at 09:01
  • Bummer, I tried sortArtist and sortAlbumArtist on iOS 14 and it just crashes. Apparently it's not available anymore. – Germán Mar 14 '21 at 20:33
  • I just checked, it’s still working fine on my end. I’m guessing you might have used NSObject’s valueForKey instead of MPMediaEntity’s valueForProperty method. Using valueForKey could lead to this crash. – Casper Mar 15 '21 at 21:21