Sorting an array of songs by Album, when Album info available

点点圈 提交于 2019-12-11 07:00:12

问题


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)


回答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]];
}];


来源:https://stackoverflow.com/questions/21609105/sorting-an-array-of-songs-by-album-when-album-info-available

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!