0

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!

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.