是否有模拟 - (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 ×1