Skip to content

Instantly share code, notes, and snippets.

@phatblat
Last active May 8, 2016 06:13
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 phatblat/1224d6cebba4c2d0cd42eed20a099603 to your computer and use it in GitHub Desktop.
Save phatblat/1224d6cebba4c2d0cd42eed20a099603 to your computer and use it in GitHub Desktop.
An example of the Schwartzian Transform in Swift
// 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