Created
February 3, 2017 21:42
-
-
Save rockarts/9ba3022ec137c4ad9f713c136690665e to your computer and use it in GitHub Desktop.
Index Words by First Letter in Swift
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
| func buildIndexFromNames(names: [String]) -> [String:[String]] { | |
| var indexedNames: [String:[String]] = [String():[]] | |
| for name in names{ | |
| let firstLetterOfString = String(name.characters.prefix(1)).uppercaseString | |
| if indexedNames[firstLetterOfString] != nil { | |
| indexedNames[firstLetterOfString]!.append(name) | |
| } else { | |
| indexedNames[firstLetterOfString] = [name] | |
| } | |
| } | |
| return indexedNames | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment