I need to sort a bunch of objects based on an integer that is stored in an NSString
.
I know this is one solution and it works:
NSSortDescriptor *mySortDescriptor = [NSSortDescriptor sortDescriptorWithKey: @"from" ascending: YES comparator:^(id obj1, id obj2)
{
if ([obj1 integerValue] > [obj2 integerValue])
{
return (NSComparisonResult) NSOrderedDescending;
}
if ([obj1 integerValue] < [obj2 integerValue])
{
return (NSComparisonResult) NSOrderedAscending;
}
return (NSComparisonResult) NSOrderedSame;
}];
self.myObjects = [[self.data allObjects] sortedArrayUsingDescriptors: @[mySortDescriptor]];
My question is, why can't I use KVO for this, it looks much cleaner, eg like this:
NSSortDescriptor *mySortDescriptor = [NSSortDescriptor sortDescriptorWithKey: @"from.integerValue" ascending: YES];
And then pass that to my NSFetchRequest
With this code, 200 appears before 21.