Using Swift 2.1 I'm trying to make a function that sorts an array of dictionaries by the date value for the key dateKey.
I want to add this as an extension to the Array type, so I can call it using someArray.sortDictionariesByDate(dateKey: String, dateFormatter: NSDateFormatter)
extension Array where Element: CollectionType {
mutating func sortDictionariesByDate(dateKey: String, dateFormatter: NSDateFormatter) {
sortInPlace {
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZ"
if let dateStringA: String = ($0 as? [String: AnyObject])?[dateKey] as? String, let dateStringB = ($1 as? [String: AnyObject])?[dateKey] as? String {
if let dateA: NSDate = dateFormatter.dateFromString(dateStringA), dateB: NSDate = dateFormatter.dateFromString(dateStringB) {
return dateA.compare(dateB) == .OrderedAscending
}
}
return false
}
}
}
This works fine as long as the dictionaries are typed as [String: AnyObject]
, but if I use it on a dictionary of type [String: String]
it doesn't work since it's unable to cast String
to AnyObject
. I assume this is because String
is a Struct
not a Class
. I've also tried to typecast the elements to [String: Any]
instead, but then it won't work regardless of using dictionaries of type [String: AnyObject]
or [String: String]
.
Is there any cast I can use to support dictionaries with key type String
and any value type (String
, AnyObject
etc.), or perhaps a where clause or protocol conformance that can be added to the extension to avoid casting completely?
EDIT: Here are two example arrays as per request
var sampleArray1: [[String: AnyObject]] = [["date": "2015-10-24T13:00:00.000Z", "foo": "bar"], ["date": "2015-10-24T14:00:00.000Z", "foo": "bar"]]
sampleArray1.sortDictionariesByDate("date", dateFormatter: NSDateFormatter())
var sampleArray2: [[String: String]] = [["date": "2015-10-24T13:00:00.000Z", "foo": "bar"], ["date": "2015-10-24T14:00:00.000Z", "foo": "bar"]]
sampleArray2.sortDictionariesByDate("date", dateFormatter: NSDateFormatter())