Search code examples
ioscore-datansmanagedobjectnsmutabledictionary

Sort array of NSManagedObjects based on second array


I am trying to sort an array of NSManagedObject based on values associated to each of them. These values are expensive to compute so I want to compute them at most once. Most solutions for similar questions involve constructing a NSDictionary but my object does not (necessarily) conform to NSCopying protocol. I have thought about using the object id but is there maybe a cleaner/simpler solution ? Is there any catch with this method ?


Solution

  • I've found an embarrassingly simple solution to my problem. I can just create a dictionary whose keys are the indices of my objects in their array, and whose associated value are those that are associated with the objects. I sort the dictionary by value and then retrieve the objects in the order given by the keys.