简体   繁体   English

Swift中的字典键如何排序?

[英]How Are Dictionary Keys Sorted In Swift?

Examine the following dictionary: 检查以下字典:

var testDict: [String : Int] = [
    "a" : 1,
    "b" : 2,
    "c" : 3,
    "d" : 4
]

Now, iterate through the keys of the dictionary: 现在,遍历字典的键:

for i in testDict.keys {
    print(i)
}

This generates the following output: 这将产生以下输出:

bacd 百特

However, the expected output would be the keys in the coded order: 但是,预期的输出将是编码顺序中的键:

abcd A B C D

As this is not the case, I assume that the keys are sorted. 因为不是这种情况,所以我假设键已排序。 If so, how are they sorted, and more importantly, how can they be returned to the order in which they were written? 如果是这样,如何对它们进行排序,更重要的是,如何将它们返回到编写时的顺序?

From Apple's docs : 苹果的文档

Unlike items in an array, items in a dictionary do not have a specified order 与数组中的项目不同,字典中的项目没有指定的顺序

In short, dictionary items are not sorted, they are stored as efficiently as possible by their hashValue (you should not make any assumptions over the order), that's why: 简而言之,字典项没有排序,它们通过其hashValue尽可能有效地存储(您不应对顺序进行任何假设),这就是为什么:

A dictionary Key type must conform to the Hashable protocol, like a set's value type. 字典键类型必须符合哈希协议,例如集合的值类型。

To your second question, since a dictionary is not sorted, you cannot return the values or keys inside to the original order (since it didn't have any). 第二个问题是,由于未对字典进行排序,因此您无法将内部的值或键返回到原始顺序(因为它没有任何顺序)。 What you can do is iterate through all values and keys in a specific order, like this: 您可以执行的操作是按特定顺序遍历所有值和键,如下所示:

let dic = ["a" : 1, "b" : 2, "c" : 3, "d" : 4]
for key in dic.keys.sort(<) {
    print("key: \(key), value: \(dic[key])")
}

A dictionary (Swift or NSDictionary ) is a key-value map. 字典(Swift或NSDictionary )是键值映射。 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. 如果要订购字典,则代码的结果将是: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. 将同NSArray有序集合作为NSArray保留,即使没有办法对其进行排序:考虑一个包含字符串,数字和图像的数组。 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 . (再次:无论排序意味着什么。)在Swift,Objective-C和Foundation的整个生物圈中,只有一个排序的集合: NSIndexSet

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM