-3

I have NSArray of custom objects with id property and an another NSArray of NSString. The second array is a list of ids. So I want to get array of object arranged by order in second array.

3
  • You've created a custom object with a @property ... id? Sounds like a bad idea in Objective-C. Anyway, what have you tried? Commented Sep 9, 2013 at 5:58
  • @trojanfoe, it should be any other property, for example, name. I have a list of names and i want to arrange a list of objects with order of list of names. Commented Sep 9, 2013 at 6:01
  • Compare stackoverflow.com/a/15944419/1187415 or stackoverflow.com/a/16518619/1187415. Commented Sep 9, 2013 at 6:45

1 Answer 1

3

use an NSSortDescriptor, or sort with a block that performs the comparison in the other array.

How to sort an NSMutableArray with custom objects in it?

Update:

Since neither the objects nor the list of ids have a reference to the other, there is no efficient way to reorder. Without introducing a new collection, you will have to iterate through one of the lists many many times.

The only way you can do this efficiently is to change your architecture and use a hash map of some kind such as an NSDictionary so you can quickly reference an object from the id. Alternatively you can use an ordered dictionary.

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

3 Comments

I saw this question. But this methods compare objects in array to be sorted. I want to arrange objects by order in external array. I'm afraid of running throw all the items
I can't think of a straight forward way to efficiently reorder them unless you put your objects into an NSDictionary keyed by their id. Then create a new array by looping through your array of ids, grab the object from the hashtable and push it into the new array.
yes, I don't see any pretty solution, that's why I've asked a question here

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.