Swift 3, Sort an Array of Dictionaries
<p>I'm having a heck of a time in Swift 3 sorting an array of dictionaries.</p>

<p>In Swift 2, I would do it this way, which worked fine:</p>

<pre><code>var dicArray = [Dictionary&lt;String, String&gt;()]
let dic1 = ["last": "Smith", "first": "Robert"]
dicArray.append(dic1)
let dic2 = ["last": "Adams", "first": "Bill"]
dicArray.append(dic2)
let sortedArray = dicArray.sort { ($0["last"] as? String) &lt; ($1["last"] as? String) }
</code></pre>

<p>Converting the same code to Swift 3 has not gone well. The system guided me to this (through a circuitous route):</p>

<pre><code>let sortedArray = dicArray.sorted { ($0["last"]! as String) &lt; ($1["last"]! as String) }
</code></pre>

<p>But the app always crashes, with the error that it found nil while unwrapping an Optional value.</p>

<p>After banging my head against the table for too long, putting ?s and !s in every imaginable combination, I resorted to an old approach to get the job done:</p>

<pre><code>let sortedArray = (dicArray as NSArray).sortedArray(using: [NSSortDescriptor(key: "last", ascending: true)]) as! [[String:AnyObject]]
</code></pre>

<p>That works, and I'm moving along, but it's not very Swifty, is it?</p>

<p>Where did it all go wrong? How can I make the pure Swift sort function work in a case like this?</p>