当从sqlite数据表中获得数据的时候,可能希望按照某列排序,可以使用
NSSortDescriptor达到目的:
下面是例子代码:
SMutableArray *QuestionArray = [[NSMutableArrayalloc] init];
NSManagedObjectContext *context = [selfmanagedObjectContext];
NSFetchRequest *fetchRequest = [[NSFetchRequestalloc] init];
NSEntityDescription *entity = [NSEntityDescriptionentityForName:@”Question”inManagedObjectContext:context];
// sort the result
NSSortDescriptor *sortByQuestion = [[NSSortDescriptoralloc]initWithKey:@”question”ascending:YES
selector:@selector(caseInsensitiveCompare:)
];
[fetchRequest setSortDescriptors:[NSArrayarrayWithObject:sortByQuestion]];
[fetchRequest setEntity:entity];
NSError *error;
NSArray *fetchedObjects = [context executeFetchRequest:fetchRequest error:&error];
for (Question *question in fetchedObjects) {
[QuestionArray addObject:question];
}