gooberboobbutt gooberboobbutt - 1 year ago 110
Swift Question

How to arrange by numeric order highest to lowest from value in [NSMutableDictionary]?

var ditto:[NSMutableDictionary] = [["name":"Bob", "rank":14], ["name":"Randy", "rank":5], ["name":"Julie", "rank":20]]

How can I sort ditto by numeric order, high to low, according to "rank"? This is the result I want:

var ditto:[NSMutableDictionary] = [["name":"Julie", "rank":20],["name":"Bob", "rank":14], ["name":"Randy", "rank":5]]

Answer Source

Here's the code

ditto.sortInPlace { ($0.0["rank"] as! Int) > ($0.1["rank"] as! Int) }

However you should really use a model value like this

struct Person {
    let name: String
    let rank: Int

You should also stop using the old fashion way NSMutableDictionary and start using the Swift dictionary... you are programming in Swift don't you :)

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download