Skip to content

Instantly share code, notes, and snippets.

@BasThomas
Created June 25, 2015 22:28
Show Gist options
  • Save BasThomas/fd293a383fb6a70dc536 to your computer and use it in GitHub Desktop.
Save BasThomas/fd293a383fb6a70dc536 to your computer and use it in GitHub Desktop.
Sorting
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