Sort with NSSortDescriptor

2

I am trying to show in UITableViewController my items stored in Core Data in a specific order given by one of its attributes. There is an attribute that can be 1 or 0 and my intention is to show first those whose value is 1.

NSFetchRequest *req = [NSFetchRequest fetchRequestWithEntityName:@"MyContacts"];

req.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"value" ascending:YES]];

NSFetchedResultsController *results = [[NSFetchedResultsController alloc]initWithFetchRequest:req managedObjectContext:self.Model.context sectionNameKeyPath:nil cacheName:nil];

ContactsViewController *cVC = [[ContactsViewController alloc]initWithFetchedResultsController:results style:UITableViewStylePlain]';

This is my code, but it does not sort the list.

Any help would be great.

    
asked by jescabias 16.12.2015 в 10:47
source

1 answer

2

If you want the 1 to appear in the order then the 0 should be with ascending:NO :

req.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"value" ascending:NO]];

Also, what is @"value" ? There you should put the property name of MyContacts for which you want to order (which may be called value ...)

    
answered by 16.12.2015 в 13:03