Skip to content

Instantly share code, notes, and snippets.

@SAllen0400
Created April 3, 2017 05:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SAllen0400/d9b6fa973b92c6c0c8f6440997520241 to your computer and use it in GitHub Desktop.
Save SAllen0400/d9b6fa973b92c6c0c8f6440997520241 to your computer and use it in GitHub Desktop.
Swift Snippet #2
class Player {
let name: String
let highScore: Int
init(name: String, highScore: Int) {
self.name = name
self.highScore = highScore
}
}
let curry = Player(name: "Curry", highScore: 58)
let harden = Player(name: "Harden", highScore: 53)
let james = Player(name: "James", highScore: 62)
let westbrook = Player(name: "Westbrook", highScore: 56)
let jordan = Player(name: "Jordan", highScore: 9000)
var players: [Player] = [curry, harden, james, westbrook, jordan]
let highScores = players.sorted(by: { $0.highScore > $1.highScore })
for player in highScores {
print("\(player.name) score = \(player.highScore)")
}
players.sort(by: { $0.name < $1.name })
for player in players {
print(player.name)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment