ios - 对数组中的数据进行排序

标签 ios objective-c sorting core-data

我有一个来自 Core Data 的数组,我想使用我添加到数据库(Scene 实体)的 orderNum 字段对其进行排序。

我提出以下请求来获取数据:

NSArray *scenes;

NSManagedObjectContext *context = [self managedObjectContext];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];

NSEntityDescription *entity = [NSEntityDescription entityForName:@"Scenes" inManagedObjectContext:context];

[fetchRequest setEntity:entity];
NSError *error;

scenes = [context executeFetchRequest:fetchRequest error:&error];
return scenes;

如何按 orderNumscenes 中返回的数据进行排序?

最佳答案

使用NSSortDescriptororderNum作为 key并将该排序描述符应用于您的 fetchRequest .

NSSortDescriptor *sort = [NSSortDescriptor sortDescriptorWithKey:@"orderNum" ascending:YES];
[fetchRequest setSortDescriptors:@[ sort ]];

关于ios - 对数组中的数据进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22716246/

相关文章:

ios - 使用 Xcode 中的部分从数组的数组中分配一个值

Java程序排序随机数组

php - 在php中对带有特殊字符的多维数组进行排序

ios - 使用 Bluemix iOS Client api 为汽车入门应用程序更新 IoT 上的设备数据

ios - UITableViewCell 中的 UILabel - 切断信息

ios - 我们可以使用 google api 绘制路线(没有多段线)吗?

objective-c - Objective-C : Stripping HTML attributes from a string

ios - 禁用具有单元可重用性的 UITableViewCell 中的 UIButton

iphone - 在矩形上画圆

python - 在 Python 中将项目插入不区分大小写的排序列表