I recently encountered some problems while trying to sort a set of objects.
The objects I’d like to sort are subclasses of NSManagedObject.
I want to sort the objects by a ‘global_index’, which is, however, not a property in my model. It’s just a getter -(NSInteger)globalIndex {...} each of the objects implements. Inside this method, I do some complex calculation which cannot be done with a simple sort descriptor.
Now, my question: Is there a way to make an NSSortDescriptor sort the objects by the return value of a method?
I really want to use a sort descriptor because it’s (IMO) the only way to make use of NSFetchedResultsController’s cool features. Or is there a way to tell the controller how to sort the fetched objects? Like…
- (NSArray *)sortObjects:(NSSet *)objects {...}
Thanks in advance!
According to the Core Data Programming Guide,
The easy workaround is to save the order with each object (which is a pain when you need to insert an object between two others, but that’s a problem with trying to implement an efficient ordered collection in a database).