Ios – How to sort strings in NSMutableArray into alphabetical order

cocoa-touchiosnsmutablearrayobjective csorting

I have a list of strings in an NSMutableArray, and I want to sort them into alphabetical order before displaying them in my table view.

How can I do that?

Best Answer

There is the following Apple's working example, using sortedArrayUsingSelector: and localizedCaseInsensitiveCompare: in the Collections Documentation:

sortedArray = [anArray sortedArrayUsingSelector:
                       @selector(localizedCaseInsensitiveCompare:)];

Note that this will return a new sorted array. If you want to sort your NSMutableArray in place, then use sortUsingSelector: instead, like so:

[mutableArray sortUsingSelector:@selector(localizedCaseInsensitiveCompare:)];