如何你理清元组的数组? [英] How to you sort an Array of Tuples?
问题描述
如何实现(或创建)一个元组列表的数组排序?
以下是从我的code收集。
基本上我创建的元组数组
并通过循环填充它;之后,我试图排序。
VAR myStringArray:(字符串,整数)[]? =零
...myStringArray + =(实物,数字)
...myStringArray.sort {$ 0 GT; $ 1}
这是X $ C $ç给我,我才建:
test.swift:57:9:'(字符串,整数)[]'没有一个名为构件
排序
块引用>解决方案您有两个问题。首先,
myStringArray
是可选
,必须将解包之前,你可以调用它的方法。其次,没有>
运营商的元组,你必须做的比较自己如果myStringArray {
myStringArray!的.sort {$ 0.0次== $ 1.0吗? $ 0.1 GT; $ 1.1:$ 0.0次> $ 1.0的}
}How do you implement (or create) an array sort of a list of tuples?
The following was gleaned from my code.
Essentially I created an array of tuples and populated it via for loop; after which I tried to sort it.var myStringArray: (String,Int)[]? = nil ... myStringArray += (kind,number) ... myStringArray.sort{$0 > $1}
This is what Xcode gave me before I could build:
test.swift:57:9: '(String, Int)[]?' does not have a member named 'sort'
解决方案You have two problems. First,
myStringArray
is anOptional
, you must "unwrap" it before you can call methods on it. Second, there is no>
operator for tuples, you must do the comparison yourselfif myStringArray { myStringArray!.sort { $0.0 == $1.0 ? $0.1 > $1.1 : $0.0 > $1.0 } }
这篇关于如何你理清元组的数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!