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.
如何实现(或创建)一组元组列表的数组?从我的代码中收集了以下内容。基本上我创建了一个元组数组并通过for循环填充它;之后我试着对它进行排序。
var myStringArray: (String,Int)[]? = nil
...
myStringArray += (kind,number)
...
myStringArray.sort{$0 > $1}
This is what Xcode gave me before I could build:
这是Xcode在我构建之前给了我的:
test.swift:57:9: '(String, Int)[]?' does not have a member named 'sort'
test.swift:57:9:'(String,Int)[]?'没有名为'sort'的成员
2 个解决方案
#1
11
You have two problems. First, myStringArray
is an Optional
, you must "unwrap" it before you can call methods on it. Second, there is no >
operator for tuples, you must do the comparison yourself
你有两个问题。首先,myStringArray是一个可选项,您必须先“解包”它,然后再调用它上面的方法。其次,元组没有>运算符,你必须自己进行比较
if myStringArray {
myStringArray!.sort { $0.0 == $1.0 ? $0.1 > $1.1 : $0.0 > $1.0 }
}
#2
6
Actually, what I was looking for, is the tuple with the largest integer value:
实际上,我正在寻找的是具有最大整数值的元组:
var myStringArray: (String,Int)[]? = nil
...
println("myStringArray: \(myStringArray)\n")
myStringArray!.sort {$0.1 > $1.1}
println("myStringArray: \(myStringArray)\n")
...
Original:
原版的:
myStringArray: [(One, 1), (Square, 1), (Square, 4), (Square, 9), (Square, 16), (Square, 25), (Prime, 2), (Prime, 3), (Prime, 5), (Prime, 7), (Prime, 11), (Prime, 13), (Fibonacci, 1), (Fibonacci, 1), (Fibonacci, 2), (Fibonacci, 3), (Fibonacci, 5), (Fibonacci, 8)]
myStringArray:[(One,1),(Square,1),(Square,4),(Square,9),(Square,16),(Square,25),(Prime,2),(Prime,3) ,(Prime,5),(Prime,7),(Prime,11),(Prime,13),(Fibonacci,1),(Fibonacci,1),(Fibonacci,2),(Fibonacci,3),( Fibonacci,5),(Fibonacci,8)]
Sorted:
排序方式:
myStringArray: [(Square, 25), (Square, 16), (Prime, 13), (Prime, 11), (Square, 9), (Fibonacci, 8), (Prime, 7), (Prime, 5), (Fibonacci, 5), (Square, 4), (Prime, 3), (Fibonacci, 3), (Prime, 2), (Fibonacci, 2), (One, 1), (Square, 1), (Fibonacci, 1), (Fibonacci, 1)]
myStringArray:[(Square,25),(Square,16),(Prime,13),(Prime,11),(Square,9),(Fibonacci,8),(Prime,7),(Prime,5) ,(Fibonacci,5),(Square,4),(Prime,3),(Fibonacci,3),(Prime,2),(Fibonacci,2),(One,1),(Square,1),( Fibonacci,1),(Fibonacci,1)]
...so it's the "square" having the largest integer: 25.
...所以它是具有最大整数的“正方形”:25。
#1
11
You have two problems. First, myStringArray
is an Optional
, you must "unwrap" it before you can call methods on it. Second, there is no >
operator for tuples, you must do the comparison yourself
你有两个问题。首先,myStringArray是一个可选项,您必须先“解包”它,然后再调用它上面的方法。其次,元组没有>运算符,你必须自己进行比较
if myStringArray {
myStringArray!.sort { $0.0 == $1.0 ? $0.1 > $1.1 : $0.0 > $1.0 }
}
#2
6
Actually, what I was looking for, is the tuple with the largest integer value:
实际上,我正在寻找的是具有最大整数值的元组:
var myStringArray: (String,Int)[]? = nil
...
println("myStringArray: \(myStringArray)\n")
myStringArray!.sort {$0.1 > $1.1}
println("myStringArray: \(myStringArray)\n")
...
Original:
原版的:
myStringArray: [(One, 1), (Square, 1), (Square, 4), (Square, 9), (Square, 16), (Square, 25), (Prime, 2), (Prime, 3), (Prime, 5), (Prime, 7), (Prime, 11), (Prime, 13), (Fibonacci, 1), (Fibonacci, 1), (Fibonacci, 2), (Fibonacci, 3), (Fibonacci, 5), (Fibonacci, 8)]
myStringArray:[(One,1),(Square,1),(Square,4),(Square,9),(Square,16),(Square,25),(Prime,2),(Prime,3) ,(Prime,5),(Prime,7),(Prime,11),(Prime,13),(Fibonacci,1),(Fibonacci,1),(Fibonacci,2),(Fibonacci,3),( Fibonacci,5),(Fibonacci,8)]
Sorted:
排序方式:
myStringArray: [(Square, 25), (Square, 16), (Prime, 13), (Prime, 11), (Square, 9), (Fibonacci, 8), (Prime, 7), (Prime, 5), (Fibonacci, 5), (Square, 4), (Prime, 3), (Fibonacci, 3), (Prime, 2), (Fibonacci, 2), (One, 1), (Square, 1), (Fibonacci, 1), (Fibonacci, 1)]
myStringArray:[(Square,25),(Square,16),(Prime,13),(Prime,11),(Square,9),(Fibonacci,8),(Prime,7),(Prime,5) ,(Fibonacci,5),(Square,4),(Prime,3),(Fibonacci,3),(Prime,2),(Fibonacci,2),(One,1),(Square,1),( Fibonacci,1),(Fibonacci,1)]
...so it's the "square" having the largest integer: 25.
...所以它是具有最大整数的“正方形”:25。