开发者

numeric sort with NSSortDescriptor for NSFetchedResultsController

开发者 https://www.devze.com 2023-02-04 13:39 出处:网络
I\'m trying to numerically sort data that\'s displayed in a UITableView. Before that I used such a sort descriptor:

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 w开发者_运维问答hy they won't sort the data in the same way the first method did...

Any ideas?


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

0

精彩评论

暂无评论...
验证码 换一张
取 消