I have a custom data object with a NSNumber property. How would I sort the messages from one array into another array using the NSNumber property?
I tried this but I get an error
Binary operator '<' cannot be applied to two 'NSNumber?' operands
emptyArray = messagesArray.sort{$0.time < $1.time}
Ascending or descending doesn't matter
class Message{
var toMessage: String?
var time: NSNumber?
}
var messagesArray[Message]()
let message1 = Message()
message1.toMessage = "Bonjour"
message1.time = 123
let message2 = Message()
message1.toMessage = "Hola"
message1.time = 456
let message3 = Message()
message1.toMessage = "Hello"
message1.time = 789
messagesArray.append(message1)
messagesArray.append(message2)
messagesArray.append(message3)
//I need to get the elements from messagesArray into emptyArray sorting by NSNumber time property
var emptyArray = [Message]()
我需要使用intValue
emptyArray = messagesArray.sort{$0.time.intValue < $1.time.intValue}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.