2014-10-06 84 views
1

我目前有一個[String: (Int, NSDate)]類型的字典。我想得到一個數組(在這種情況下是字符串),按數組中的NSDate排序。基於與快捷鍵相關的元組值的字典鍵排序

到目前爲止,我可以取得最好的是這樣的代碼:

var ret = sorted(self.dictionary, { 
    var tuple1 = $0.1 as (Int, NSDate) 
    var tuple2 = $1.1 as (Int, NSDate) 
    return tuple1.1.compare(tuple2.1) == NSComparisonResult.OrderedAscending 
}) 

但返回以下類型的東西:[(String, (Int, NSDate))]

有我檢索數組的簡單方法只有字符串?

回答

3

ret = sorted(self.dictionary, ...)返回數組(key,value)元組。你可以用

let keys = map(ret) { $0.0 } 

或者提取的按鍵,按鍵排序基於其在字典中的值:

let keys = sorted(self.dictionary.keys) { 
    (key1, key2) in 
    let tuple1 = self.dictionary[key1]! 
    let tuple2 = self.dictionary[key2]! 
    return tuple1.1.compare(tuple2.1) == NSComparisonResult.OrderedAscending 
}