Last active
December 29, 2017 17:44
-
-
Save mattyoung/49ebd775801d877d9bb7 to your computer and use it in GitHub Desktop.
weheartswift.com Dictionary Excercise 11.6 Leaderboard
This file contains hidden or 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
| 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