Swift按键排序字典数组,其中value是可选的AnyObject

Fra*_*kie 8 arrays sorting ios swift pfobject

我正在直接从Parse中提取一系列字典并将它们显示在表格中.所以我真的很想处理我所掌握的数据结构(下面奇怪的结构化词典).

A PFObject[String : AnyObject?],我希望能够按任意键排序,所以我不知道对象类型,并且某些字典可能缺少密钥.因为在Parse中,如果不给属性赋值,则它根本就不存在.例如:

[
    {
        "ObjectId" : "1",
        "Name" : "Frank",
        "Age" : 32
    },
    {
        "ObjectId" : "2",
        "Name" : "Bill"
    },
    {
        "ObjectId" : "3",
        "Age" : 18
    }
    {
        "ObjectId" : "4",
        "Name" : "Susan",
        "Age" : 47
    }
Run Code Online (Sandbox Code Playgroud)

]

我希望在排序的词典之后始终按顺序排列缺少键的词典.一个例子:

原表:

ObjectId   Name       Age
1          Frank      32
2          Bill     
3                     18
4          Susan      47
Run Code Online (Sandbox Code Playgroud)

按名称排序:

ObjectId   Name       Age
2          Bill       
1          Frank      32
4          Susan      47
3                     18
Run Code Online (Sandbox Code Playgroud)

由于我对数据模型没有太多控制权,并且它的使用在整个应用程序中受到限制,我更倾向于关注算法解决方案而不是结构化.

我想出了一个方法来做到这一点,但它似乎效率低而且速度慢,我确信有人可以做得更好.

//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
Run Code Online (Sandbox Code Playgroud)

谢谢!

Cod*_*ent 7

Swift无法比较任何两个对象.您必须先将它们转换为特定类型:

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)
Run Code Online (Sandbox Code Playgroud)

如果有多个字典没有指定的值key,它们将被放置在result数组的末尾,但它们的相对顺序是不确定的.


Luc*_*tti 6

要求

所以你有一系列词典.

let dictionaries: [[String:AnyObject?]] = [
    ["Name" : "Frank", "Age" : 32],
    ["Name" : "Bill"],
    ["Age" : 18],
    ["Name" : "Susan", "Age" : 47]
]
Run Code Online (Sandbox Code Playgroud)

您想要对数组进行排序:

  • 随着Name价值上升
  • 没有a的词典Name String应该在最后

这是代码(在函数式编程风格中)

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
}
Run Code Online (Sandbox Code Playgroud)

产量

print(sorted)

[
    ["Name": Optional(Bill)],
    ["Name": Optional(Frank), "Age": Optional(32)],
    ["Name": Optional(Susan), "Age": Optional(47)],
    ["Age": Optional(18)]
]
Run Code Online (Sandbox Code Playgroud)