Last active
March 30, 2016 07:49
-
-
Save Qata/40c2c4064acbd3529665b1176763c7a9 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 A = ("A" as NSString).characterAtIndex(0) | |
let Z = ("Z" as NSString).characterAtIndex(0) | |
let dictionaries: [[String : [String]]] = (A...Z).map { | |
Character(UnicodeScalar($0)) | |
}.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