Sort NSArray of custom objects based on sorting of another NSArray of strings

Posted by Nic Hubbard on Stack Overflow See other posts from Stack Overflow or by Nic Hubbard
Published on 2012-11-20T09:02:04Z Indexed on 2012/11/20 17:00 UTC
Read the original article Hit count: 380

I have two NSArray objects that I would like to be sorted the same. One contains NSString objects, the other custom Attribute objects. Here is what my "key" NSArray looks like:

// The master order
NSArray *stringOrder = [NSArray arrayWithObjects:@"12", @"10", @"2", nil];

The NSArray with custom objects:

// The array of custom Attribute objects that I want sorted by the stringOrder array
NSMutableArray *items = [[NSMutableArray alloc] init];
Attribute *attribute = nil;

attribute = [[Attribute alloc] init];
attribute.assetID = @"10";
[items addObject:attribute];

attribute = [[Attribute alloc] init];
attribute.assetID = @"12";
[items addObject:attribute];

attribute = [[Attribute alloc] init];
attribute.assetID = @"2";
[items addObject:attribute];

So, what I would like to do is use the stringOrder array to determine the sorting of the items array of custom objects. How can I do this?

© Stack Overflow or respective owner

Related posts about objective-c

Related posts about ios