1

I'm populating an array with songs that match a specific criteria.

  NSArray *songs = [[[MPMediaQuery alloc] init] items];
  NSMutableArray *filteredSongs;
  for (int i=0; i<[songs count]; i++) {
       // filter logic here: if songs[i] match the criteria, push it filteredSongs array

  }

Now I'd like to sort that array by MPMediaItemPropertyAlbumTitle, but avoiding problems if the Album info of the user's mp3 is missing. (I'm trying to find a workaround for the problem solved here)

Community
  • 1
  • 1
Sr.Richie
  • 5,680
  • 5
  • 38
  • 62
  • Hmm, so what's the problem? If the album info is missing, just remove those songs from the array before you sort. – matt Feb 06 '14 at 16:57
  • mmmm no, i need to show them all, sorting them by album if the info exists – Sr.Richie Feb 06 '14 at 16:58
  • I didn't say don't show them all. I said sort without the ones that lack the album info. Then put those ones back in. I have no idea where you think they go, or how you think they should be ordered; you did not specify your input / desired output. All I'm saying is that this is trivial. – matt Feb 06 '14 at 17:29
  • looks like the answer by @Jared it's working :) – Sr.Richie Feb 06 '14 at 17:54
  • If it's working, can you please select it as the answer so people searching later on can find it easier. – Jared Messenger Feb 06 '14 at 19:12
  • of course @Jared , today I'll finish all the tests to confirm it and I'll mark it as ok – Sr.Richie Feb 07 '14 at 09:29

1 Answers1

1

You can use the NSComparisonResult to sort by album title. This should handle nil albumTitle values and push them to the back.

[songs sortUsingComparator:^NSComparisonResult(id obj1, id obj2){
return [[obj2 albumTitle] compare:[obj1 albumTitle]];
}];
Jared Messenger
  • 1,236
  • 1
  • 13
  • 14