0

I have a NSMutableArray of objects, below is the struct:

//object at index i
locations {
   NSString* address;
   NSString* state;
   NSNumber* distance;
}

I have 10000 object like the above structure in the NSMutableArray. How do order this array so that the locations are in order by the NSNumber distance?

I tried this:

lowToHigh = [NSSortDescriptor sortDescriptorWithKey:@"distance" ascending:YES];
[locationArray sortUsingDescriptors:[NSArray arrayWithObject:lowToHigh]];

is using the sortDescriptorWithKey:@"distance" wrong? since distance isn't actually a key, it NSNumber* distance.

edit in my header @property (strong, nonatomic)NSNumber* _distance; and then @synthesize _distance = distance; in my methods file but this is in locationObjects.* object class. Then I import this in my current class I am doing this sorting. Is that an issue? How I import is locationObjects* locObj = [[locationObjects alloc] init];

John Riselvato
  • 12,854
  • 5
  • 62
  • 89
  • If you have a property `-(NSString*)distance` in addition to ivar `distance`, your trick should just work. – Sergey Kalinichenko Jun 27 '12 at 15:12
  • @HotLicks it makes the array only have one object 10000 times. and dasblinkenlight its a NSNumber – John Riselvato Jun 27 '12 at 15:14
  • Did you alloc/init the actual lowToHigh NSSortDescriptor? (The 'Specifying Sorts Using NSSortDescriptor' section on this doc http://developer.apple.com/library/ios/#DOCUMENTATION/Cocoa/Conceptual/SortDescriptors/Concepts/Creating.html#//apple_ref/doc/uid/20001845-BAJEAIEE) – tarheel Jun 27 '12 at 15:17
  • yes of course, @tarheel. – John Riselvato Jun 27 '12 at 15:18

1 Answers1

1

This is what I use:

NSSortDescriptor *lowToHigh = [[NSSortDescriptor alloc] initWithKey:@"distance"
                                              ascending:YES];
NSArray *mySortDescriptors = [NSArray arrayWithObject:lowToHigh];
NSArray *sortedArray = [locationArray sortedArrayUsingDescriptors:mySortDescriptors];
Firo
  • 15,448
  • 3
  • 54
  • 74
  • The only difference between what I tried and what you posted is the `sortedArrayUsingDescriptors` vs `sortUsingDescriptors`. Which unfortunately gives me the same exact sorting as I did before posting this question. – John Riselvato Jun 27 '12 at 15:22
  • Sorry I was primarily referring to alloc/initing them, but I now see the comment asking about it. Besides that they are the exact same. – Firo Jun 27 '12 at 15:25
  • I think my issue is I don't have a key called distance only an NSNumber called distance. – John Riselvato Jun 27 '12 at 15:27
  • @JohnRiselvato -- You mean you never defined the `@property` called distance, I suspect. – Hot Licks Jun 27 '12 at 15:27
  • nope I do, in my header `@property (strong, nonatomic)NSNumber* _distance;` and then `@synthesize _distance = distance;` in my methods file – John Riselvato Jun 27 '12 at 15:30
  • 1
    Look at this, I think he is trying to do almost the exact same thing except with an NSDate: http://stackoverflow.com/questions/805547/how-to-sort-an-nsmutablearray-with-custom-objects-in-it – Firo Jun 27 '12 at 15:32
  • and @joel you posted practically the same answer as him. and it doesn't work in this case. I tried following that before i posted this question actually, unfortunately. – John Riselvato Jun 27 '12 at 15:38
  • Yes, I am aware of that too (to my understanding it is the standard way of doing this). I just have never tried his other implementations and figured I would just send you there. I am sorry they did not work. – Firo Jun 27 '12 at 15:44
  • I have a feeling, I must be doing something wrong thats completely way from what my original question has asked. +1 and accepted this as the answer for all the hard work. – John Riselvato Jun 27 '12 at 15:55
  • Thanks John and if I figure something out I will be sure to let you know! – Firo Jun 27 '12 at 15:59
  • @Joel guess what my data inside the array was all the same. I found out I wasn't releasing my data after it was put into the array, and for what ever reason, when I added new data into the array, it was really pushing the retained data from the first time the values were set. So I was correct with my sorting, and so were you. thanks – John Riselvato Jun 27 '12 at 16:12
  • @JohnRiselvato Oh wow! Thank you for letting me know, I will make sure I keep that in the back of my mind! I am glad it is working! – Firo Jun 27 '12 at 16:18