Ios NSSortDescriptor问题
我目前正在对一个名为“视频”的实体进行排序。我正在尝试对所有视频进行排序,首先是按录制日期排序,格式为“2013年12月21日”,然后是标题,这只是一个字符串。以下是我正在使用的代码:Ios NSSortDescriptor问题,ios,objective-c,cocoa-touch,core-data,nssortdescriptor,Ios,Objective C,Cocoa Touch,Core Data,Nssortdescriptor,我目前正在对一个名为“视频”的实体进行排序。我正在尝试对所有视频进行排序,首先是按录制日期排序,格式为“2013年12月21日”,然后是标题,这只是一个字符串。以下是我正在使用的代码: - (void)viewWillAppear:(BOOL)animated { self.title = [NSString stringWithFormat:@"Videos of %@", _currentAthlete.full]; AppDelegate *appDelegate =
- (void)viewWillAppear:(BOOL)animated {
self.title = [NSString stringWithFormat:@"Videos of %@", _currentAthlete.full];
AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
_managedObjectContext = [appDelegate managedObjectContext];
NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSEntityDescription *video = [NSEntityDescription entityForName:@"Video" inManagedObjectContext:_managedObjectContext];
[request setEntity:video];
NSSortDescriptor *sortDescriptor =
[[NSSortDescriptor alloc] initWithKey:@"date_recorded"
ascending:YES
selector:@selector(localizedCaseInsensitiveCompare:)];
NSSortDescriptor *sortDescriptor2 =
[[NSSortDescriptor alloc] initWithKey:@"title"
ascending:YES
selector:@selector(localizedCaseInsensitiveCompare:)];
NSArray *sortDescriptors = [[NSArray alloc]initWithObjects:sortDescriptor, sortDescriptor2, nil];
[request setSortDescriptors:sortDescriptors];
NSError *error = nil;
NSMutableArray *mutableFetchResults = [[_managedObjectContext executeFetchRequest:request error:&error] mutableCopy];
if (mutableFetchResults == nil) {
//handle error
}
[self setVideoArray:mutableFetchResults];
[self.tableView reloadData];
}
但是,这是表格数据的显示顺序:
Cell 1: Title @"Z", date recorded: @"December 14, 2013"
Cell 2: Title @"A", date recorded: @"December 21, 2013"
Cell 3: Title @"B", date recorded: @"December 7, 2013"
Cell 4: Title @"T", date recorded: @"December 8, 2013"
我做错什么了吗?知道为什么会这样吗?如果我做得对,它可能是CellForRowatineXpath中的某个东西吗?将它们作为字符串排序,因此顺序是这样的。如果记录的
date\u
字段类型是一个字符串,我认为通过核心数据(如果有的话)按字符串字段的日期值进行查询是不容易的
您可以考虑将字段类型更改为DATE类型,或者在查询了来自核心数据的所有数据之后,订购数据记录。