Skip to content

Instantly share code, notes, and snippets.

@Qata
Created March 30, 2016 06:42
Show Gist options
  • Save Qata/6426d2bd8ffb992b7860481329719e8c to your computer and use it in GitHub Desktop.
Save Qata/6426d2bd8ffb992b7860481329719e8c to your computer and use it in GitHub Desktop.
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