I am trying to sort an array
of Dictionaries
by their timeStamp
value from newest to oldest
error: Binary operator '<' cannot be applied to two 'Any?' operands
timeStamp
value is saved as an Int
into the Dictionary
:
timeStamp = inviteDict["timeStamp"] as? Int
Dictionary:
[
"type": "invite",
"complimentId": key,
"status": status,
"timeStamp": timeStamp!,
"fromUser": user
]
Function - combines two arrays
of tuples, I need to sort the array
by timeStamp
before passing via handler
:
func getInvitesAndCompliments(forUserId forId: String, handler: @escaping ([[String : Any]], Bool) -> ()){
var invitesAndCompliments = [[String : Any]]()
var invites = [[String : Any]]()
var compliments = [[String : Any]]()
getComplimentsReceived(forUserId: forId) { (complimentsDict, success) in
invitesAndCompliments.removeAll()
compliments = complimentsDict
invitesAndCompliments = invites + compliments
handler(invitesAndCompliments, true)
}
getInvitesReceived(forUserId: forId) { (invitesDict, success) in
invitesAndCompliments.removeAll()
invites = invitesDict
invitesAndCompliments = compliments + invites
//sort array by timeStamp value
let sorted_invitesAndCompliments = invitesAndCompliments.sorted(by: { $0["timeStamp"] < $1["timeStamp"] })
handler(sorted_invitesAndCompliments, true)
}
}//end func
The result of getting a value from a dictionary is always Any?
, you have to downcast the types
let sorted_invitesAndCompliments = invitesAndCompliments.sorted(by: { ($0["timeStamp"] as! Int) < $1["timeStamp"] as! Int })
To avoid that use a custom struct.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments