【发布时间】:2014-08-13 19:26:20
【问题描述】:
我基本上是在尝试实现NSSortDescriptor with first name and last name,or with either first name or last name?,但使用的是 NSFetchedResultsController。我正在从通讯录中读取记录,并希望像通讯录一样显示它们。
记录将根据姓氏的第一个字母分类。如果没有姓氏,它将按名字排序,如果没有名字,它将按公司排序。
目前我有
- (NSFetchedResultsController *)fetchedResultsController {
if (_fetchedResultsController != nil) {
return _fetchedResultsController;
}
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
// read the contact list
NSEntityDescription *entity = [Contact MR_entityDescriptionInContext:[NSManagedObjectContext MR_defaultContext]];
[fetchRequest setEntity:entity];
// sort the results by last name then by first name
NSSortDescriptor *sort1 = [[NSSortDescriptor alloc] initWithKey:@"lastName" ascending:YES selector:@selector(caseInsensitiveCompare:)];
NSSortDescriptor *sort2 = [[NSSortDescriptor alloc] initWithKey:@"firstName" ascending:YES selector:@selector(caseInsensitiveCompare:)];
[fetchRequest setSortDescriptors:@[sort1, sort2]];
// Fetch all the contacts and group them by the firstLetter in the last name. We defined this method in the CoreData/Human/Contact.h file
NSFetchedResultsController *theFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:[NSManagedObjectContext MR_defaultContext] sectionNameKeyPath:@"firstLetter" cacheName:nil];
self.fetchedResultsController = theFetchedResultsController;
_fetchedResultsController.delegate = self;
return _fetchedResultsController;
}
先按姓再按名字排序。我将如何更改它以返回上面列出的所需结果?
【问题讨论】:
标签: ios objective-c sorting nsfetchedresultscontroller