我有一组元组(
var array = [("John Doe", 13), ("The Guy", 15), ("The Person", 19)]
并希望按从最大到最小的int值对它们进行排序。最后,我希望数组看起来像这样
var array = [("The Person", 19), ("The Guy", 15), ("John Doe", 13)]
我将如何做到这一点?
答案 0 :(得分:7)
var array = [("John Doe", 13), ("The Guy", 15), ("The Person", 19)]
print("myStringArray: \(array)\n")
array.sort {$0.1 > $1.1}
print("myStringArray: \(array)\n")
答案 1 :(得分:1)
您必须通过元组的Int值的$1
值进行比较,检查此代码,对您有所帮助
var tuplesArray : [(String,Int)] = [("John Doe", 13), ("The Guy", 15), ("The Person", 19)]
let orderedTuples = tuplesArray.sorted { (value1, value2) -> Bool in
return value1.1 > value2.1
}
我希望这有助于你