Reputation: 5823
for example, if I have an object:
@interface MyObject
{
__id;
__data;
}
and I have an NSArray of such object:
id=4
data=Apple
id=5
data=Banana
id=6
data=Orange
I also have an NSArray of ids:
{"5", "4", "6"}
now I want to sort the array of objects by ids to be in the order in the second array, so the result should be:
id=5
data=Banana
id=4
data=Apple
id=6
data=Orange
Is it possible(in ObjC for iPhone)? What is the most efficient way?
Thanks!
Upvotes: 0
Views: 77
Reputation: 11970
You can use - (NSArray *)sortedArrayUsingComparator:(NSComparator)cmptr
function of the NSArray class.
You should write your own comparator block that will return ordering depending on the position of id in the ids array - ie return NSOrderedAscending when idA is further from the start of the array than idB.
Hope this helps.
Upvotes: 1