【发布时间】:2016-08-31 05:57:14
【问题描述】:
我有一个包含一些核心数据对象的数组。每个对象都有三个属性,例如“First Name”和“Last Name”
我想根据搜索字母对数组进行过滤和排序。最终列表应按以下顺序排列:
- 以名字开头的记录应该放在第一位
- 以姓氏开头的记录应该排在第二位
- 包含名字的记录应该排在第三位
- 包含姓氏的记录应该排在第四位
我在创建谓词和排序描述符时感到困惑。有人可以帮忙吗?
编辑:
这是我获取的结果控制器:
-(NSFetchedResultsController *)myFetchedResultsController
{
if (!_myFetchedResultsController)
{
...
...
NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:MY_ENTITY_NAME];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(firstName beginswith[cd] %@) || (lastName beginswith[cd] %@) || (firstName contains[cd] %@) || (lastName contains[cd] %@))", self.searchText, self.searchText, self.searchText, self.searchText];
[fetchRequest setPredicate:predicate];
//I really dont have idea about how to set sort description to this scenario
NSSortDescriptor *firstNameDescriptors = [[NSSortDescriptor alloc] initWithKey:@"firstName" ascending:YES selector:@selector(localizedCaseInsensitiveCompare:)];
NSSortDescriptor *lastNameDescriptors = [[NSSortDescriptor alloc] initWithKey:@"lastName" ascending:YES selector:@selector(localizedCaseInsensitiveCompare:)];
[fetchRequest setSortDescriptors:@[firstNameDescriptors, lastNameDescriptors]];
_userFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:appDelegate.managedObjectContext sectionNameKeyPath:nil cacheName:nil];
}
return _myFetchedResultsController;
}
但这似乎不起作用,意味着它不对对象进行排序。
【问题讨论】:
-
能否请您展示您尝试过的方法以及您遇到的问题?
标签: objective-c sorting nsarray filtering nspredicate