Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I sort strings in NSMutableArray into alphabetical order?

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?

like image 499
praveena Avatar asked Jun 11 '11 10:06

praveena


People also ask

How do you sort a string in alphabetical order?

Using the toCharArray() method Get the required string. Convert the given string to a character array using the toCharArray() method. Sort the obtained array using the sort() method of the Arrays class. Convert the sorted array to String by passing it to the constructor of the String array.

How do you arrange an array of strings in alphabetical order?

JavaScript Array sort() The sort() sorts the elements of an array. The sort() overwrites the original array. The sort() sorts the elements as strings in alphabetical and ascending order.

Is Nsmutablearray ordered?

The answer is yes, the order of the elements of an array will be maintained - because an array is an ordered collection of items, just like a string is an ordered sequence of characters...

How do you sort an array in Objective C?

The trick to sorting an array is a method on the array itself called "sortedArrayUsingDescriptors:". The method takes an array of NSSortDescriptor objects. These descriptors allow you to describe how your data should be sorted.


2 Answers

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:)]; 
like image 187
Vijay-Apple-Dev.blogspot.com Avatar answered Oct 01 '22 22:10

Vijay-Apple-Dev.blogspot.com


Here's an updated answer for Swift:

  • Sorting can be done in Swift with the help of closures. There are two methods - sort and sorted which facilitate this.

    var unsortedArray = [ "H", "ello", "Wo", "rl", "d"] var sortedArray = unsortedArray.sorted { $0.localizedCaseInsensitiveCompare($1) == NSComparisonResult.OrderedAscending } 

    Note: Here sorted will return a sorted array. The unsortedArray itself will not be sorted.

  • If you want to sort unsortedArray itself, use this

    unsortedArray.sort { $0.localizedCaseInsensitiveCompare($1) == NSComparisonResult.OrderedAscending } 


Reference :
  • Here is the documentation for Swift's sorting methods.

  • Docs for different String comparison methods here.


Optional:

Instead of using localizedCaseInsensitiveCompare, this could also be done:

stringArray.sort{ $0.lowercaseString < $1.lowercaseString } 

or even

stringArray.sort{ $0 < $1 } 

if you want a case sensitive comparison

like image 31
aksh1t Avatar answered Oct 01 '22 22:10

aksh1t