I have an array of dictionaries called groupedDictionary defined below:
// The type is [String : [SingleRepository]]
let groupedDictionary = Dictionary(grouping: finalArrayUnwrapped) { (object) -> String in
var language = "Not Known to GitHub"
if let languageUnwrapped = object.language {
language = languageUnwrapped
}
return language
}
I can easily get all the keys as follows:
let keys = groupedDictionary.keys
However, when I try to sort this array using sorted(by:) in Swift 4, I successfully get the sorted array back with the type [(key: String, value: [SingleRepository])].
// the type is [(key: String, value: [SingleRepository])]
let sortedGroupedDictionary = groupedDictionary.sorted(by: { ($0.value.count) > ($1.value.count) })
How can I get all of the keys from sortedGroupedDictionary?
It is not possible to call ".keys" on sortedGroupedDictionary, since it has a different type.
Please note: I'm not trying to sort the array based on the keys. I did sort the array that consists of dictionaries, based on a predicate which is size of the array containing each value, now I just want to extract the keys.
答案 0 :(得分:1)
方法$ find . -type f \( -iname \*.js -o -iname \*.css \) -exec gzip -9 "{}" \; -exec mv "{}.gz" "{}" \; -exec sed 's#^./##' "{}" \;
以键-值对数组的形式返回原始字典的键和值,并按您作为参数传递的谓词进行排序。这意味着每个元组的第一个元素就是您要查找的键。
您可以像这样查看结果:
Dictionary.sorted(by:)
如果您需要的只是一组排序键,则可以使用
for (key, value) in sortedGroupedDictionary {
// handle this key-value-pair
}