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

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

问题描述

我刚刚接触到苹果对Core Data的iPhone 3.0 SDK支持,我对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?

例如:
农场有1到多对牛仔(Farm.cowboys)
牛仔有1对1 )
具有lastName(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显示牛仔。现在,如何按lastName对牛仔进行排序?我需要重新获取它们吗?如果是的话,我如何使用嵌套排序?

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?

目前我正在排序一个NSMutableArray牛仔([Farm.cowboys allobjects]),这是罚款为那些属性在牛仔。但是我不知道如何在通过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天全站免登陆