Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
weheartswift.com Dictionary Excercise 11.6 Leaderboard
var people: [[String:Any]] = [
[
"firstName": "Calvin",
"lastName": "Newton",
"score": 13
],
[
"firstName": "Garry",
"lastName": "Mckenzie",
"score": 23
],
[
"firstName": "Leah",
"lastName": "Rivera",
"score": 10
],
[
"firstName": "Sonja",
"lastName": "Moreno",
"score": 3
],
[
"firstName": "Noel",
"lastName": "Bowen",
"score": 16
]
]
func >(lhs: [String:Any], rhs: [String:Any]) -> Bool {
guard let lhsScore = lhs["score"] as? Int,
let rhsScore = rhs["score"] as? Int else {
return false
}
return lhsScore > rhsScore
}
var sorted = people.sort(>)
for (index, person) in sorted.enumerate() {
guard let firstName = person["firstName"],
let lastName = person["lastName"],
let score = person["score"] else {
break
}
print("\(index + 1). \(firstName) \(lastName) - \(score)")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment