Frankie
Frankie

Reputation: 11928

Swift sort array of dictionaries by key where value is optional AnyObject

I'm pulling an array of dictionaries straight from Parse and displaying them in a table. So I'd really like to work with the data structure I'm handed (the oddly structured dictionaries below).

A PFObject is [String : AnyObject?] and I want to be able to sort by any key so I don't know the object type AND the key might be missing from some of the dictionaries. Because in Parse, if you don't give a property a value, it is simply nonexistent. For example:

[
    {
        "ObjectId" : "1",
        "Name" : "Frank",
        "Age" : 32
    },
    {
        "ObjectId" : "2",
        "Name" : "Bill"
    },
    {
        "ObjectId" : "3",
        "Age" : 18
    }
    {
        "ObjectId" : "4",
        "Name" : "Susan",
        "Age" : 47
    }

]

I want the dictionaries with missing keys to always be ordered after the sorted dictionaries. An example:

Original Table:

ObjectId   Name       Age
1          Frank      32
2          Bill     
3                     18
4          Susan      47

Ordered By Name:

ObjectId   Name       Age
2          Bill       
1          Frank      32
4          Susan      47
3                     18

As I don't have a lot of control over the data model, and it's usage is limited throughout the application, I'd prefer to focus on an algorithmic solution rather than structural.

I came up with a way to do this but it just seems inefficient and slow, I'm certain there's someone who can do this better.

//dataModel is an array of dictionary objects used as my table source
//sort mode is NSComparisonResult ascending or descending
//propertyName is the dictionary key

        //first filter out any objects that dont have this key
        let filteredFirstHalf = dataModel.filter({ $0[propertyName] != nil })
        let filteredSecondHalf = dataModel.filter({ $0[propertyName] == nil })

        //sort the dictionaries that have the key
        let sortedAndFiltered = filteredFirstHalf { some1, some2 in

            if let one = some1[propertyName] as? NSDate, two = some2[propertyName] as? NSDate {
                return one.compare(two) == sortMode
            } else if let one = some1[propertyName] as? String, two = some2[propertyName] as? String {
                return one.compare(two) == sortMode
            } else if let one = some1[propertyName] as? NSNumber, two = some2[propertyName] as? NSNumber {
                return one.compare(two) == sortMode
            } else {
                fatalError("filteredFirstHalf shouldn't be here")
            }
        }

        //this will always put the blanks behind the sorted
        dataModel = sortedAndFiltered + filteredSecondHalf

Thanks!

Upvotes: 8

Views: 5929

Answers (4)

jscs
jscs

Reputation: 64012

Make a datatype to represent your data:

struct Person 
{
    let identifier: String
    let name: String?
    let age: Int?
}

Make an extraction routine:

func unpack(objects: [[String : Any]]) -> [Person]
{
    return objects.flatMap { object in

        guard let identifier = object["ObjectID"] as? String else {
            // Invalid object
            return nil
        }
        let name = object["Name"] as? String
        let age = object["Age"] as? Int

        return Person(identifier: identifier, name: name, age: age)
    }
}

Your datatype can be sorted by its fields, because they have real types.

let objects: [[String : Any]] = 
               [["ObjectID" : "1", "Name" : "Frank", "Age" : 32],
                ["ObjectID" : "2", "Name" : "Bill"],
                ["ObjectID" : "3", "Age" : 18],
                ["ObjectID" : "4", "Name" : "Susan", "Age" : 47]]

let persons = unpack(objects)

let byName = persons.sort { $0.name < $1.name }

nils compare as "before" any other value; you can write your own comparator if you'd like to change that.

Upvotes: 0

Sethmr
Sethmr

Reputation: 3084

Here is what I would do. If you are able to, I would make the struct more specific by giving it a Name and Age other than just key and value. This should give you an outline for how to achieve that though!

struct PersonInfo {
    var key: String!
    var value: AnyObject?

    init(key key: String, value: AnyObject?) {
        self.key = key
        self.value = value
    }
}

class ViewController: UIViewController {
    var possibleKeys: [String] = ["Name", "Age", "ObjectId"]
    var personInfos: [PersonInfo] = []
    override func viewDidLoad() {
        super.viewDidLoad()
        for infos in json {
            for key in possibleKeys {
                if let value = infos[key] {
                    personInfos.append(PersonInfo(key: key, value: value))
                }
            }
        }
        personInfos.sortInPlace({$0.value as? Int > $1.value as? Int})
    }
}

to make it easier, here:

struct PersonInfo {
    var key: String!
    var objectId: Int!
    var name: String?
    var age: Int? 

    init(key key: String, objectId: Int, name: String?, age: Int?) {
        self.key = key
        self.objectId = objectId
        self.name = name
        self.age = age
    }
}

class ViewController: UIViewController {
    var possibleKeys: [String] = ["Name", "Age", "ObjectId"]
    var personInfos: [PersonInfo] = []
    override func viewDidLoad() {
        super.viewDidLoad()
        for infos in json {
            var objectId: String!
            var name: String? = nil
            var age: Int? = nil
            for key in possibleKeys {
                if let value = infos[key] {
                    if key == "ObjectId" {
                        objectId = value as? String
                    }
                    if key == "Name" {
                        name = value as? String
                    }
                    if key == "Age" {
                        age = value as? Int
                    }
                }
            }
            personInfos.append(PersonInfo(key: key, objectId: objectId, name: String?, age: Int?))
        }
        //by objectId
        personInfos.sortInPlace({$0.objectId? > $1.objectId?})

        //by age
        personInfos.sortInPlace({$0.age? > $1.age?})

        //byName
        personInfos.sortInPlace({$0.name?.compare($1.name?) == NSComparisonResult.OrderedAscending})
    }
}

Upvotes: -1

Code Different
Code Different

Reputation: 93191

Swift can't compare any two objects. You have to cast them to a specific type first:

let arr: [[String: Any]] = [
    ["Name" : "Frank", "Age" : 32],
    ["Name" : "Bill"],
    ["Age" : 18],
    ["Name" : "Susan", "Age" : 47]
]

let key = "Name" // The key you want to sort by

let result = arr.sort {
    switch ($0[key], $1[key]) {
        case (nil, nil), (_, nil):
            return true
        case (nil, _):
            return false
        case let (lhs as String, rhs as String):
            return lhs < rhs
        case let (lhs as Int, rhs as Int):
            return  lhs < rhs
        // Add more for Double, Date, etc.
        default:
            return true
    }
}

print(result)

If there are multiple dictionaries that have no value for the specified key, they will be placed at the end of the result array but their relative orders are uncertain.

Upvotes: 8

Luca Angeletti
Luca Angeletti

Reputation: 59536

Requirements

So you have an array of dictionaries.

let dictionaries: [[String:AnyObject?]] = [
    ["Name" : "Frank", "Age" : 32],
    ["Name" : "Bill"],
    ["Age" : 18],
    ["Name" : "Susan", "Age" : 47]
]

You want to sort the array:

  • with the Name value ascending
  • dictionaries without a Name String should be at the end

Solution

Here's the code (in functional programming style)

let sorted = dictionaries.sort { left, right -> Bool in
    guard let rightKey = right["Name"] as? String else { return true }
    guard let leftKey = left["Name"] as? String else { return false }
    return leftKey < rightKey
}

Output

print(sorted)

[
    ["Name": Optional(Bill)],
    ["Name": Optional(Frank), "Age": Optional(32)],
    ["Name": Optional(Susan), "Age": Optional(47)],
    ["Age": Optional(18)]
]

Upvotes: 7

Related Questions