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.
Asked
Active
Viewed 95 times
-3

Valentin Shamardin
- 3,569
- 4
- 34
- 51
-
You've created a custom object with a `@property ... id`? Sounds like a bad idea in Objective-C. Anyway, what have you tried? – trojanfoe Sep 09 '13 at 05: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. – Valentin Shamardin Sep 09 '13 at 06:01
-
Compare http://stackoverflow.com/a/15944419/1187415 or http://stackoverflow.com/a/16518619/1187415. – Martin R Sep 09 '13 at 06:45
1 Answers
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.
-
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 – Valentin Shamardin Sep 09 '13 at 05:51
-
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. – Levi Sep 09 '13 at 06:06
-
yes, I don't see any pretty solution, that's why I've asked a question here – Valentin Shamardin Sep 09 '13 at 06:10