Skip to content

Instantly share code, notes, and snippets.

@gaplo917
Created May 20, 2018 18:10
Show Gist options
  • Save gaplo917/687269b02c6376b148076a18c8ba9bc8 to your computer and use it in GitHub Desktop.
Save gaplo917/687269b02c6376b148076a18c8ba9bc8 to your computer and use it in GitHub Desktop.
Swift Sorted List Question
//: Playground - noun: a place where people can play
struct Post {
let isLiked: Bool
let timestamp: Int64
let content: String
}
extension Post: Equatable {
static func == (lhs: Post, rhs: Post) -> Bool {
return lhs.isLiked == rhs.isLiked &&
lhs.timestamp == rhs.timestamp &&
lhs.content == rhs.content
}
}
let posts = [
Post(isLiked: false, timestamp: 8, content: "8"),
Post(isLiked: true, timestamp: 7, content: "7"),
Post(isLiked: false, timestamp: 6, content: "6"),
Post(isLiked: false, timestamp: 5, content: "5"),
Post(isLiked: false, timestamp: 4, content: "4"),
Post(isLiked: true, timestamp: 3, content: "3"),
Post(isLiked: false, timestamp: 2, content: "2"),
Post(isLiked: true, timestamp: 1, content: "1")
]
let expected = [
Post(isLiked: true, timestamp: 7, content: "7"),
Post(isLiked: true, timestamp: 3, content: "3"),
Post(isLiked: true, timestamp: 1, content: "1"),
Post(isLiked: false, timestamp: 8, content: "8"),
Post(isLiked: false, timestamp: 6, content: "6"),
Post(isLiked: false, timestamp: 5, content: "5"),
Post(isLiked: false, timestamp: 4, content: "4"),
Post(isLiked: false, timestamp: 2, content: "2")
]
func sortFunc(_ posts: [Post]) -> [Post] {
// TODO: implement sorting logic
return []
}
let sorted = sortFunc(posts)
print(sorted == expected)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment