A dictionary (Swift or NSDictionary
) is a key-value map. There is no order by concept. You put in one value (key) and get out another value (value) for that key. This does not depend on any order.
If you want to have an order of keys, get the keys and sort them.
BTW: There is a difference between ordered and sorted. Ordered means, that an element keeps it place. This order can be unsorted, whatever sorting means.
To show the difference think of a dictionary creation like this:
var testDict: [String : Int] = [
"b" : 2,
"a" : 1,
"c" : 3,
"d" : 4
]
If a dictionary would be ordered, the result of your code would be: b, a, c, d. This is the order the dictionary was created. But it is not sorted. (Sort how? Phonetic? Literally? Alphanumeric?)
Arrays, which are ordered (aka keeps the place of an element) are not sorted, too. Having a homogene ordered collection as NSArray
an order is kept, even if there is no way to sort them: Think of an array which contains strings, numbers and images. The array keeps the order you put the items in. But it has no idea to sort it. (Again: Whatever sorting means.) In the whole biosphere of Swift, Objective-C, and Foundation there is only one sorted collection: NSIndexSet
.