Last active
February 28, 2018 07:35
-
-
Save vialyx/89090913cf1fa935020ca2202e577a9a to your computer and use it in GitHub Desktop.
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
// 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