Sorting NSSets of a core data entity - Objective-c

前端 未结 2 1730
感动是毒
感动是毒 2020-12-31 08:57

I would like to sort the data of a core data NSSet (I know we can do it only with arrays but let me explain...). I have an entity user who has a relationship to-many with th

2条回答
  •  北海茫月
    2020-12-31 09:33

    You need to sort the recipes first:

    NSArray *sortDescriptors = [NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"name" ascending:YES]];
    
    NSArray *sortedRecipes = [[recipes allObjects] sortedArrayUsingDescriptors:sortDescriptors];
    

    you can then extract an array of attributes from the sorted recipes array and the results will remain in sorted order:

    NSArray *sortedNames = [sortedRecipes valueForKey:@"name"];
    NSArray *sortedIdentities = [sortedRecipes valueForKey:@"identity"];
    

提交回复
热议问题