Swift:排序字典 [英] Swift: A sorted dictionary

查看:171
本文介绍了Swift:排序字典的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习迅速,试图了解词典。我习惯了PHP,在那里你可以写下面的...

  $ dataPoints = Array(A=> 1, B=大于2, C=→3); 
foreach($ dataPoints as $ value){
echo($ value);
}

在此示例中,值将按顺序输出 - 1,2, 3



我的快速代码看起来像这样...

  var dataPoints:Dictionary< String,Int> = [A:1,B:2,C:3] 
数据点中的(键,值){
print(value)
}

但是,这些值以意想不到的顺序出现。有没有什么可以做的,可以按照创建的顺序保留值,还是可以快速排除字典?

解决方案

p>由于已经得到回答,词典的意思是它没有被排序。 Swift中有三种类型的集合(和Objective-C)



数组是一个有序的项目列表。当项目的顺序很重要时,请使用此选项。



字典是键和值的无序集合。当您想要有效地检索基于密钥的值时,请使用此选项。



一套是无序,唯一的项目。当您想要按照特定顺序收集唯一项目时,请使用此选项。



对于您的情况,您的订单似乎对您而言非常重要,但您希望存储某些值的对。也许你应该看的是使用一系列元组:



这样的东西,也许

  let dataPoints = [(A,1),(B,2),(C,3)] 

(letter,number)在dataPoints {
print(\(letter),\(number))
}

哪个输出

  A,1 
B,2
C,3

另外



如果您在创建之前不知道项目的顺序,但是有一个基于键的自然排序,您可以这样做:

  let dict = [
A:1,
B:2,
C:3
]

for dict.keys.sort(){
guard let value = dict [key] else {break}
print(\(key),\ ))
}

在这种情况下,您将获得一个带有默认排序的键数组订单(你可以使用不同的nt sort函数,如果有其他要求),并输出基于排序顺序的键值。


I'm learning swift and trying to understand dictionaries. I'm used to PHP, where you might write the following...

$dataPoints = Array("A"=>1,"B"=>2,"C"=>3);
foreach($dataPoints as $value) {
    echo($value);
}

In this example, the values would be output in order - 1, 2, 3

My swift code looks like this...

var dataPoints: Dictionary<String,Int> = ["A":1,"B":2,"C":3]
for (key, value) in dataPoints {
        print(value)
}

However, the values come out in an unexpected order. Is there something clean I can do to keep the values in the order they were created, or can a swift dictionary never be sorted?

解决方案

As has already been answered, the point of a dictionary is that it is not sorted. There are three types of collections in Swift (and Objective-C)

An array is an ordered list of items. Use this when the order of the items is important.

A dictionary is an unordered collection of keys and values. Use this when you want to efficiently retrieve a value based on a key.

A set is a bag of unordered, unique items. Use this when you want to have a collection of unique items in no particular order.

For your case it seems that the ordering is what is important to you, but you want to store some kind of pair of values. Perhaps what you should be looking at is using an array of tuples:

something like this, maybe

let dataPoints = [("A", 1), ("B", 2), ("C", 3)]

for (letter, number) in dataPoints {
    print("\(letter), \(number)")
}

which outputs

A, 1
B, 2
C, 3

Alternatively

If you don't know about the order of the items before their creation, but there is a natural ordering based on the keys, you could do something like this:

let dict = [
    "A" : 1,
    "B" : 2,
    "C" : 3
]

for key in dict.keys.sort() {
    guard let value = dict[key] else { break }
    print("\(key), \(value)")
}

In this case you get an array of keys with their default sort order (you can use a different sort function if you have other requirements) and output the key values based on that sorted order.

这篇关于Swift:排序字典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆