1

I can enumerate through ALAssetsGroupAlbum and create a NSMutableArray sorted by the album name as below without an issue. But, WHAT I REALLY WANT to do is to sort the albums in the same way iPhone's Photo apps does, which is to follow the manual order set on iPhoto, which may not be alphabetical. I'm wondering if Assets Library holds this order information somewhere. Does anyone has an idea how Apple does that? Thanks.


ALAssetsLibraryGroupsEnumerationResultsBlock listGroupBlock = ^(ALAssetsGroup *group, BOOL *stop) {

    ALAssetsFilter *onlyPhotosFilter = [ALAssetsFilter allPhotos];
    [group setAssetsFilter:onlyPhotosFilter];
    if ([group numberOfAssets] > 0)
    {
   //     [self.groups addObject:group];
        [self.groups insertObject:group atIndex:0];
    }
    else
    {
        //sort groups by name
          [self.groups sortUsingComparator:^NSComparisonResult(id obj1, id obj2) {
              return [[obj1 valueForProperty:ALAssetsGroupPropertyName] localizedCaseInsensitiveCompare:[obj2 valueForProperty:ALAssetsGroupPropertyName]];
          }];

        [self.tableView performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO];
    }
2
  • Hi, I'm also looking to display albums in the same order as the Photos.app, any updates ?
    – manar
    Nov 7, 2014 at 9:59
  • Unfortunately, not. And, I stopped looking, too. I decided to divide them into sections, such as Albums, Events and Faces, and sort them alphabetically. It doesn't look great, but acceptable for my purpose.
    – tsuyoski
    Nov 8, 2014 at 3:49

1 Answer 1

1

// This worked for me. Here's a Swift 2 version. Sort on any property. I used the Album names.

var albums:[ALAssetsGroup] = []

// enumerate to get your albums . . .

func sortAndReloadAlbums()
{
    self.albums.sortInPlace({ (album1:ALAssetsGroup, album2:ALAssetsGroup) -> Bool in
        let album1Name: String = String(album1.valueForProperty(ALAssetsGroupPropertyName))
        let album2Name: String = String(album2.valueForProperty(ALAssetsGroupPropertyName))

        return album1Name < album2Name
    })

    self.tableView.reloadData()
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.