How can I sort strings in NSMutableArray into alphabetical order?

praveena picture praveena · Jun 11, 2011 · Viewed 37.3k times · Source

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?

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:)];