I get categories from my database with this code:
db.collection(DatabaseRef.categories).document(DatabaseRef.categoriesDoc).getDocument { snap, error in
guard error == nil, let snap = snap else {
return
}
let data = snap.data()!
for (k,v) in data {
self.categories.append([Int(k) ?? 0 : v])
}
self.categoryCollectionView.reloadData()
}
Here is my categories variable:
var categories: [[Int: Any]] = []
// categories = [[1: "category1"], [3: "category3"], [2: "category2"]]
I would like to sort categories before I reload my collectionview. So the categories should look like the following instead:
// categories = [[1: "category1"], [2: "category2"], [3: "category3"]]
I tried the following:
let sortedCategories = categories.sorted{ $0.key > $1.key}
But I am getting this error: Unable to infer closure type in the current context
$0.keyis totally invalid. Why do you have a bunch of dictionaries with only a single key value pair, anyway?