是否有模拟 - (NSArray*)keysSortedByValueUsingSelector:(SEL)比较器在swift中?
如何在不转换为NSDictionary的情况下执行此操作?
我试过这个,但似乎不是一个好的解决方案.
var values = Array(dict.values)
values.sort({
$0 > $1
})
for number in values {
for (key, value) in dict {
if value == number {
println(key + " : \(value)");
dict.removeValueForKey(key);
break
}
}
}
Run Code Online (Sandbox Code Playgroud)
例:
var dict = ["cola" : 10, "fanta" : 12, "sprite" : 8]
dict.sortedKeysByValues(>) // fanta (12), cola(10), sprite(8)
Run Code Online (Sandbox Code Playgroud) 我看到swift中的闭包里面有0美元,有时他们使用1美元.究竟什么是$ 0,你可以使用其他$ x?
以下是它的使用示例.
applyMutliplication(2, {$0 * 3})
array.map({$0 + 1})
Run Code Online (Sandbox Code Playgroud)
谢谢!
let dict: [String:Int] = ["apple":5, "pear":9, "grape":1]
Run Code Online (Sandbox Code Playgroud)
如何根据Int值对字典进行排序,以便输出为:
sortedDict = ["pear":9, "apple":5, "grape":1]
Run Code Online (Sandbox Code Playgroud)
let sortedDict = sorted(dict) { $0.1 > $1.1 }
Run Code Online (Sandbox Code Playgroud)