edziubudzik
edziubudzik

Reputation: 224

numeric sort with NSSortDescriptor for NSFetchedResultsController

I'm trying to numerically sort data that's displayed in a UITableView.

Before that I used such a sort descriptor:

sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES selector:@selector(caseInsensitiveCompare:)];

now I'd like to use block to sort this numerically like this:

sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES comparator:^NSComparisonResult(id obj1, id obj2) {
    return [((NSString *)obj1) compare:(NSString *)obj2 options:NSCaseInsensitiveSearch | NSNumericSearch];
}];

but it sorts the data incorectly causing conflict with section names in NSFetchedResultsController. So I tryed to immitate the old sorting with a comparator block - just to be sure that the problem is not caused by numeric comparison. The problem is that those lines:

sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES comparator:^NSComparisonResult(id obj1, id obj2) {
    return [((NSString *)obj1) caseInsensitiveCompare:(NSString *)obj2];
}];

also cause the same error and I don't see why they won't sort the data in the same way the first method did...

Any ideas?

Upvotes: 0

Views: 1386

Answers (1)

Lily Ballard
Lily Ballard

Reputation: 185671

I don't know if this will work in a fetch request, but you could try sorting by @selector(localizedStandardCompare:).

Upvotes: 1

Related Questions