Skip to content

Instantly share code, notes, and snippets.

@jhugman
Created April 25, 2016 09:26
Show Gist options
  • Save jhugman/d3a5b26abe03fd4be53045d6aa354771 to your computer and use it in GitHub Desktop.
Save jhugman/d3a5b26abe03fd4be53045d6aa354771 to your computer and use it in GitHub Desktop.
Schwartzian Transform in Swift.
let strings = ["Peter Venkman", "Raymond Stantz", "Egon Spengler", "Dana Barrett", "Louis Tully"]
let sortedBySurname =
strings.map { ["string": $0, "surname": $0.componentsSeparatedByString(" ").last! ] }
.sort { $0["surname"]!.caseInsensitiveCompare($1["surname"]!) == .OrderedAscending }
.map { $0["string"]! }
// https://en.wikipedia.org/wiki/Schwartzian_transform
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment