0

I have array type like this :

let types = ["Notification","Type1","Type2","Type3"..."TypeN"]

And i'm using this extension to grouprBy an array by type

extension SequenceType {
    func groupBy<U : Hashable>(@noescape keyFunc: Generator.Element -> U) -> [U:[Generator.Element]] {
        var dict: [U:[Generator.Element]] = [:]
        for el in self {
            let key = keyFunc(el)
            if case nil = dict[key]?.append(el) { dict[key] = [el] }
        }
        return dict
    }
}

The problem is i'm getting weird order, The notification type is not the first element in my dictionary. Is possible to keep the same order of my types array into the dictionary ?

YouSS
  • 560
  • 7
  • 20
  • 2
    As answered by Sahil, a dictionary is *not* sortable. See also http://stackoverflow.com/questions/25377177/sort-dictionary-by-keys, http://stackoverflow.com/questions/24090016/sort-dictionary-by-values-in-swift?lq=1 and many similar Q&As. – Eric Aya Jun 13 '16 at 15:13

1 Answers1

2

From Apple docs:

Unlike items in an array, items in a dictionary do not have a specified order.

So, you can't keep them in sorted order in a dictionary.

Sahil Kapoor
  • 11,183
  • 13
  • 64
  • 87