Skip to content

Instantly share code, notes, and snippets.

@vialyx
Last active February 28, 2018 07:35
Show Gist options
  • Save vialyx/89090913cf1fa935020ca2202e577a9a to your computer and use it in GitHub Desktop.
Save vialyx/89090913cf1fa935020ca2202e577a9a to your computer and use it in GitHub Desktop.
// MARK: - Comparable
extension User: Comparable {
static func <(lhs: User, rhs: User) -> Bool {
return lhs.name < rhs.name
}
static func ==(lhs: User, rhs: User) -> Bool {
return lhs.name == rhs.name
}
}
var receivedUsers = [User(name: "Maxim Vialyx"), User(name: "Dmitry Sokorin"), User(name: "Artem Hugarin")]
// receivedUsers -> [{name "Maxim Vialyx"}, {name "Dmitry Sokorin"}, {name "Artem Hugarin"}]
var sortedUsers = receivedUsers.sorted()
// sortedUsers -> [{name "Artem Hugarin"}, {name "Dmitry Sokorin"}, {name "Maxim Vialyx"}]
// TODO: OR you can mutate receivedUsers array
receivedUsers.sort()
// receivedUsers -> [{name "Artem Hugarin"}, {name "Dmitry Sokorin"}, {name "Maxim Vialyx"}]
let userIDs = [986, 176, 385, 564]
let minID = userIDs.min()
// 176
let maxID = userIDs.max()
// 986
let sortedIDs = userIDs.sorted()
// [176, 385, 564, 986]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment