1

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

1
  • Each dictionary has more than one key, $0.key is totally invalid. Why do you have a bunch of dictionaries with only a single key value pair, anyway? Commented Apr 14, 2020 at 20:13

1 Answer 1

2

Since categories is a an array of dictionaries, $0 and $1 are dictionaries [Int: Any], not key value pairs (key: Int, value: Any), although they each contain exactly one key value pair.

Therefore, one way to access the one and only key value pair in each dictionary is .keys.first!.

categories.sorted(by: { $0.keys.first! < $1.keys.first! })

Frankly, I don't think [[Int: Any]] is a suitable data structure for categories. If there are no duplicate ks, you should just use a [Int: Any]. And you would insert the data this way:

for (k,v) in data {
    self.categories[[Int(k) ?? 0] = v
}

You can use the original code you used for sorting:

categories.sorted{ $0.key > $1.key }

And after sorting, you will get a [(key: Int, value: Any)].

Sign up to request clarification or add additional context in comments.

12 Comments

Thank you! Worked like a charm. reason I am using this [[Int: Any]] is because I was told collectionView works best with arrays; e.g. for number of rows in collection return categories.count... With that I keep them in an array so its easier to deal with in the collectionView; maybe I was told incorrectly?
Because dictionaries don't have a defined ordering, they don't have a defined first key, so I would caution against using this. The whole premise doesn't really much make sense, anyway
makes sense; thats why for the Int in the dictionary I only have one key for each so the first will always be that key. I am using numbers so with your sorting solution I can sort the categories by numbers.
@Alexander-ReinstateMonica Yeah, OP might have a bigger problem here, but first is fine in this specific case, no? According to how OP has initialised it, each dictionary only has one entry, so first is defined, right?
@user12669401 IDK who told you that, but you should clarify with them 1) Is this exactly what they meant? I suspect not 2) What are they hoping to achieve with dictionaries? In your case, I don't see why you wouldn't just use a struct, or maybe even a tuple
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.