PDA

View Full Version : Sorting NSMutableArray of NSNumber objects




aztec31
Sep 16, 2008, 02:46 AM
How would I sort a small NSMutableArray that has NSNumber objects. I want to arrange the array in ascending order so the smaller numbers are at the beginning of the array. I looked at the docs but there isn't much examples that are clear. Can someone provide an example?



aztec31
Sep 16, 2008, 03:06 AM
I kind of figured it out. I used
[myArray sortUsingSelector: @selector(compare:)];

but could someone explains what @selector means or does?

By the way this is not for a homework assignment.

robbieduncan
Sep 16, 2008, 03:11 AM
@selector gets a reference to the function/method that will be called to implement that selector name.

Fontano
Sep 16, 2008, 10:11 AM
Assuming myArray is of type NSMutableArray


NSSortDescriptor *mySorter = [[NSSortDescriptor alloc] initWithKey:@"property_or_method_of_objects" ascending:YES];
[myArray sortUsingDescriptors:[NSArray arrayWithObject:mySorter];


So take a look at the API documents for NSSortDescriptor for adjustments you may need for your specific case.