JMD
JMD

Reputation: 1590

Sorting an object based on a property of a child object

I have an object, say for example, a "car" object. Inside this car object I have another object of "tires." This tires object has a few properties, one of them being manufacturer.

I need to sort an array of cars based on the tire manufacturer.

I know how to sort the cars object based on a property inside the car object with descriptors. But how do you sort based on a property inside the tires object?

thanks

Upvotes: 0

Views: 214

Answers (1)

Tark
Tark

Reputation: 5173

Sort descriptors use KVC, so you can sort based on the tire manufacturer property with dot syntax. Something like:

NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"tires.manufacturer" ascending:YES];
NSArray *sortedArray = [cars sortedArrayUsingDescriptors:@[sortDescriptor]];

For more on KVC check here: https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/KeyValueCoding/Articles/KeyValueCoding.html

Upvotes: 6

Related Questions