Last active
May 8, 2016 06:13
-
-
Save phatblat/1224d6cebba4c2d0cd42eed20a099603 to your computer and use it in GitHub Desktop.
An example of the Schwartzian Transform in Swift
This file contains 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
// Schwartzian Transform | |
let list = ["a", "aa", "aaaa"] | |
// Create tuples with the attribute to sort on in the 2nd position | |
let sorted = list.map { ($0, Int($0.characters.count)) } | |
.sort { $0.1 > $1.1 } | |
.map { $0.0 } | |
print(sorted) | |
// The transform isn't actually necessary in Swift as the array can be sorted in place | |
let sorted2 = list.sort { $0.characters.count > $1.characters.count } | |
print(sorted2) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment