meman32 meman32 - 4 months ago 45
Objective-C Question

How Do I sort an NSMutable Array with NSNumbers in it?

I'm trying to make a high score table, and suck at arrays in objective c (actually, in general objective c is challenging for me), so I can't figure out how to sort. I'm trying to do something like this (speudocode, I'm writing this in actionscript style because I'm more comfortable with it):


But I can't figure it out... I've seen other threads about it, but their use plist files and stuff and I don't know enough objective c to learn from them.


Would you like to do that the short way?

If you have a mutable array of NSNumber instances:

NSSortDescriptor *highestToLowest = [NSSortDescriptor sortDescriptorWithKey:@"self" ascending:NO];
[mutableArrayOfNumbers sortUsingDescriptors:[NSArray arrayWithObject:highestToLowest]];

Nice and easy :)

You can also perform similar sorting with descriptors on immutable arrays, but you will end up with a copy, instead of in-place sorting.