如何通过对象图对托管对象的 NSMutableArray 进行排序 [英] How to Sort an NSMutableArray of Managed Objects through an object graph

查看:23
本文介绍了如何通过对象图对托管对象的 NSMutableArray 进行排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不熟悉 Apple 的 iPhone 3.0 SDK 对 Core Data 的支持,我对 iPhone 开发也比较陌生.我的问题是关于比文档中涵盖的更复杂"的排序.我怀疑我做出了错误的基本假设.

I'm new to Apple's iPhone 3.0 SDK support of Core Data and I'm relatively new to iPhone development. My question is on slightly more "complex" sorting than is covered in the docs. I suspect that I am making an incorrect, fundamental assumption.

如何根据包含在集合中更深几层的属性对预取的托管对象集合进行排序?

How can I sort a pre-fetched collection of managed objects by a property contained several layers deeper in the collection?

示例:农场有一对多的牛仔 (Farm.cowboys)牛仔有一对一的人(Cowboy.person)人有姓氏(Person.lastName)

Example: Farm has a 1 to many to Cowboy (Farm.cowboys) Cowboy has a 1 to 1 to Person (Cowboy.person) Person has lastName (Person.lastName)

整个对象图已经被获取,牛仔通过一个 UITableView 显示出来.现在,我如何按姓氏对牛仔进行排序?我需要重新获取它们吗?如果是,我该如何使用嵌套排序来做到这一点?

The entire object graph has been fetched and the cowboys are displayed through a UITableView. Now, how do I sort Cowboys by lastName? Do I need to re-fetch them? and if yes, how do I do this with a nested sort?

目前我正在尝试对牛仔 ([Farm.cowboys allobjects]) 的 NSMutableArray 进行排序,这对于牛仔上的那些属性来说很好.但是我不知道在按 Cowboy.person.lastName 对牛仔系列进行排序时如何进行这项工作?

Currently I'm trying to sort an NSMutableArray of cowboys ([Farm.cowboys allobjects]) which is fine for those properties on Cowboy. But I am not sure how to make this work when sorting a collection of Cowboys by Cowboy.person.lastName?

推荐答案

如果您可以面向 10.6,则可以使用块来完成此操作.像这样:

If you can target 10.6, you can accomplish this using blocks. Like so:

- (NSArray *)sortedArray {
    return [originalArray sortedArrayUsingComparator:(NSComparator)^(id obj1, id obj2){
    NSString *lastName1 = [[obj1 person] lastName];
    NSString *lastName2 = [[obj2 person] lastName];
    return [lastName1 caseInsensitiveCompare:lastName2]; }];
}

这篇关于如何通过对象图对托管对象的 NSMutableArray 进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆