Created
March 30, 2016 06:42
-
-
Save Qata/6426d2bd8ffb992b7860481329719e8c 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
func groupByFirstLetter(strings: [String]) -> [String : [String]] { | |
let dictionaries: [[String : [String]]] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".characters.map { (character) -> [String : [String]] in | |
let values = strings.filter { | |
$0.uppercaseString.hasPrefix(String(character)) | |
} | |
return [String(character) : values] | |
}.filter { $0.values.first!.count > 0 } | |
return dictionaries.reduce([:]) { (collector: [String : [String]], dictionary) -> [String : [String]] in | |
var returnCollector = collector | |
for (key, value) in dictionary { | |
returnCollector.updateValue(value, forKey: key) | |
} | |
return returnCollector | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment