如何在NSUserDefault中对NSMutableDictionary存储的数组进行排序?

时间:2016-11-14 14:19:24

标签: swift sorting nsmutablearray nsmutabledictionary

我有一个使用NSUserDefault保存的NSMutableDictionary数组,我想在每个NSMutableDictionary中基于“timestamp”键(值是一个String)对数组进行排序: 我用来检索数组的代码:

     if let chatsArray = NSUserDefaults().arrayForKey("myChatsList") as? [NSMutableDictionary]{
            print(chatsArray)
            //sort the array
            print(sortedArray)
     }

这就是我的数组的样子:

[{
    eventBadge = 1;
    eventId = "-KWE39B6Dh7rDzaF-xy4";
    lastMessage = uyfgduihgvifudhviufhvdf;
    timestamp = 20;
    unreadMessage = 2;
}, {
    eventBadge = 1;
    eventId = "-KWWc99ex8f7ksq8TR2U";
    lastMessage = uyfgduihgvifudhviufhvdf;
    timestamp = 5;
    unreadMessage = 1;
}, {
    eventBadge = 1;
    eventId = "-KWE5OYRZnZ5-p3LUotL";
    lastMessage = uyfgduihgvifudhviufhvdf;
    timestamp = 10;
    unreadMessage = 2;
}]

我尝试使用NSSDescriptor:

var descriptor: NSSortDescriptor = NSSortDescriptor(key: "timestamp", ascending: true)
chatsArray.sortedArrayUsingDescriptors([descriptor])

并且还使用类型化数组(在使用新类之后)

mutableChatsArray.sortInPlace{ ($0.compare($1.timestamp) == NSComparisonResult.OrderedDescending)}

到目前为止没有成功...... 谢谢你的帮助

3 个答案:

答案 0 :(得分:2)

Swift 3 示例

  let chatsArray = [[
        "eventBadge" : 1,
        "eventId" : "-KWE39B6Dh7rDzaF-xy4",
        "lastMessage" : "uyfgduihgvifudhviufhvdf",
        "timestamp" : 20,
        "unreadMessage" : 2,
        ], [
            "eventBadge" : 1,
            "eventId" : "-KWWc99ex8f7ksq8TR2U",
            "lastMessage" : "uyfgduihgvifudhviufhvdf",
            "timestamp" : 5,
            "unreadMessage" : 1,
        ], [
            "eventBadge" : 1,
            "eventId" : "-KWE5OYRZnZ5-p3LUotL",
            "lastMessage" : "uyfgduihgvifudhviufhvdf",
            "timestamp" : 10,
            "unreadMessage" : 2,
    ]] as [[String:Any]]

    // to get the timestamp value and store it inside an array with order
    let arrayTimeStamp = (chatsArray.flatMap{$0["timestamp"]} as! Array<Int>).sorted(by: >)
    print(arrayTimeStamp) // [20, 10, 5]

    //to order an array of dictionaries

    let TimeStamp = chatsArray.sorted{$0["timestamp"] as! Int > $1["timestamp"] as! Int }

    print(TimeStamp)

//[["eventId": "-KWE39B6Dh7rDzaF-xy4", "lastMessage":"uyfgduihgvifudhviufhvdf", "timestamp": 20, "unreadMessage": 2, "eventBadge": 1],
//["eventId": "-KWE5OYRZnZ5-p3LUotL", "lastMessage": "uyfgduihgvifudhviufhvdf", "timestamp": 10, "unreadMessage": 2, "eventBadge": 1],
//["eventId": "-KWWc99ex8f7ksq8TR2U", "lastMessage": "uyfgduihgvifudhviufhvdf", "timestamp": 5, "unreadMessage": 1, "eventBadge": 1]]

答案 1 :(得分:1)

使用NSUserDefaults会非常烦人。从默认设置中获取和设置数据时,我通常会遵循此技术。

var mutableArray = defaults.objectForKey("Your Array") as? NSMutableArray mutableArray.sortInPlace { (a, b) -> Bool in a.start!.compare(b.start!) == NSComparisonResult.OrderedAscending } defaults.setValue(mutableArray forKey: "Your Array")

答案 2 :(得分:1)

@Rob和@Fallah,感谢您的回答,我要检查一下,现在我通过使用NNSSDescriptor解决我的问题并添加一个选择器:

let descriptor: NSSortDescriptor = NSSortDescriptor(key: "timestamp", ascending: true, selector: #selector(NSString.localizedStandardCompare(_:)))
            let sortedChatsArray = (chatsArray as NSArray).sortedArrayUsingDescriptors([descriptor])