如何通过对象图对受管对象的NSMutableArray进行排序

时间:2022-06-15 16:00:22

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.

我是Apple的iPhone 3.0 SDK支持Core数据的新手,我对iPhone开发相对较新。我的问题是比文档中提到的稍微“复杂”的排序。我怀疑我做了一个不正确的基本假设。

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

如何通过包含集合中更深层次的属性对预先获取的托管对象集合进行排序?

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)

示例:Farm有1到多个牛仔(Farm.cowboys)牛仔有1到1个人(Cowboy.person)人有lastName(Person.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?

已获取整个对象图,并通过UITableView显示牛仔。现在,我如何按姓氏对牛仔队进行排序?我需要重新获取它们吗?如果是,我如何使用嵌套排序?

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?

目前我正在尝试对牛仔的NSMutableArray([Farm.cowboys allobjects])进行排序,这对牛仔的那些属性来说很好。但是我不确定如何通过Cowboy.person.lastName对Cowboys的集合进行排序时如何工作呢?

4 个解决方案

#1


21  

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

如果您可以定位10.6,则可以使用块来完成此操作。像这样:

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

#2


5  

You can sort like so:

您可以这样排序:

[myUnsortedArrayOfCowboys sortedArrayUsingDescriptors:
    [NSArray arrayWithObject:
        [NSSortDescriptor
            sortDescriptorWithKey:"person.lastName"
            ascending:YES]]];

Notice the '.' in the sort descriptor key. This is how you traverse the object graph.

注意'。'在排序描述符键中。这是您遍历对象图的方式。

#3


1  

If you want to sort number and string then use this

如果要对数字和字符串进行排序,请使用此选项

NSArray *unsortedArray = [NSArray arrayWithObjects:@"Hello", @"4", @"Hi", @"5", @"2", @"10", @"1", nil];
NSArray *sortedArray = [unsortedArray sortedArrayUsingComparator:^(id firstObject, id secondObject) {
    return [((NSString *)firstObject) compare:((NSString *)secondObject) options:NSNumericSearch];
}];

#4


0  

Hopefully someone has a better solution. However....

希望有人有更好的解决方案。然而....

A wrapper object with a compare selector and a reference to the sort object works. It is not as clean as I would like so hopefully someone else will come up with an alternative.

包含比较选择器和对sort对象的引用的包装器对象可以正常工作。它并不像我想的那么干净,所以希望其他人能想出一个替代方案。

In essence CowboyWrapper has a cowboy property. CowboyWrapper also has a compare selector. By building and keeping in sync an NSMutableArray of CowboyWrappers, sort works.

从本质上讲,CowboyWrapper有牛仔属性。 CowboyWrapper也有一个比较选择器。通过构建并保持同步一个CowboyWrappers的NSMutableArray,排序工作。

#1


21  

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

如果您可以定位10.6,则可以使用块来完成此操作。像这样:

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

#2


5  

You can sort like so:

您可以这样排序:

[myUnsortedArrayOfCowboys sortedArrayUsingDescriptors:
    [NSArray arrayWithObject:
        [NSSortDescriptor
            sortDescriptorWithKey:"person.lastName"
            ascending:YES]]];

Notice the '.' in the sort descriptor key. This is how you traverse the object graph.

注意'。'在排序描述符键中。这是您遍历对象图的方式。

#3


1  

If you want to sort number and string then use this

如果要对数字和字符串进行排序,请使用此选项

NSArray *unsortedArray = [NSArray arrayWithObjects:@"Hello", @"4", @"Hi", @"5", @"2", @"10", @"1", nil];
NSArray *sortedArray = [unsortedArray sortedArrayUsingComparator:^(id firstObject, id secondObject) {
    return [((NSString *)firstObject) compare:((NSString *)secondObject) options:NSNumericSearch];
}];

#4


0  

Hopefully someone has a better solution. However....

希望有人有更好的解决方案。然而....

A wrapper object with a compare selector and a reference to the sort object works. It is not as clean as I would like so hopefully someone else will come up with an alternative.

包含比较选择器和对sort对象的引用的包装器对象可以正常工作。它并不像我想的那么干净,所以希望其他人能想出一个替代方案。

In essence CowboyWrapper has a cowboy property. CowboyWrapper also has a compare selector. By building and keeping in sync an NSMutableArray of CowboyWrappers, sort works.

从本质上讲,CowboyWrapper有牛仔属性。 CowboyWrapper也有一个比较选择器。通过构建并保持同步一个CowboyWrappers的NSMutableArray,排序工作。