Jayesh Gyanchandani
Jayesh Gyanchandani

Reputation: 315

How to Sort using NSMutableArray?

I am trying to sort the NSMutableArray with this piece of code

let sortContacts = contactData! as NSArray as? [String]
        print("\(sortContacts.sort())") 

I am trying to convert the NSMutableArray to Array of String but when it comes to print("\(sortContacts.sort())") its giving me nil

Anyone who can suggest me sorting through NSMutableArray?

Upvotes: 0

Views: 43

Answers (2)

Tj3n
Tj3n

Reputation: 9923

This works, just make sure your NSMutableArray contains only string, else use if let:

let contactData = NSMutableArray(array: ["c","b","a"])
let sortContacts = contactData as NSArray as! [String]
print(sortContacts.sort())

Upvotes: 0

dasdom
dasdom

Reputation: 14063

The class NSMutableArray has several sort methods. You should read the documentation. One of the sort methods is this one.

By the way: a ! in Swift code is a code smell.

Upvotes: 1

Related Questions