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

前端之家收集整理的这篇文章主要介绍了ios – 按键键入字典的Swift排序数组,其中value是可选的AnyObject前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在直接从Parse中提取一系列字典并将它们显示在表格中.所以我真的很想处理我所掌握的数据结构(下面奇怪的结构化词典).

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

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

]

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

原表:

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

名称排序:

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

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

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

//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

谢谢!

解决方法

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)

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

原文链接:/iOS/328702.html

猜你在找的iOS相关文章