so basically like the title says is there a way to use NSSortDescriptor to sort the results of a NSFetchRequest based on a specific attributes (that is a string) length with respect to number of characters?
For example
Order of results would be:
A
AA
AAA
AAAA
and so on
Thanks a lot everyone! I can’t figure this one out
This is the sort descriptor i was trying:
NSSortDescriptor* sort = [NSSortDescriptor sortDescriptorWithKey:@"food_name" ascending:YES comparator:^(id obj1, id obj2) {
NSInteger len1 = [obj1 length];
NSInteger len2 = [obj2 length];
if (len1 < len2) return NSOrderedAscending;
if (len1 > len2) return NSOrderedDescending;
return NSOrderedSame;
}];
I put a breakpoint in it and it doesn’t seem to be getting called.
You can write a sort descriptor with a custom comparator. Blocks make it really easy!