Created
June 25, 2015 22:28
-
-
Save BasThomas/fd293a383fb6a70dc536 to your computer and use it in GitHub Desktop.
Sorting
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
struct Message { | |
var timestamp: Double | |
} | |
extension Message: Printable { | |
var description: String { | |
return "Message with timestamp \(self.timestamp)" | |
} | |
} | |
var someMessages = [Message(timestamp: 30), Message(timestamp: 20)] | |
someMessages.sort({ $0.timestamp < $1.timestamp }) // is the same as... | |
print(someMessages) /* "[Message with timestamp 20.0, Message with timestamp 30.0]" */ | |
someMessages.sort({ (message1: Message, message2: Message) -> Bool in | |
return message1.timestamp < message2.timestamp }) // this one. :-) | |
print(someMessages) /* "[Message with timestamp 20.0, Message with timestamp 30.0]" */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment