import UIKit
var arr:[Int] = [1,3,5,7,9,2,4,6,8,0]
sorted(arr)
func compareTwoInts(a:Int,b:Int)->Bool{
return a>b
}
sorted(arr,compareTwoInts)
sorted(arr,{(a:Int,b:Int)->Bool in
return a>b
})
var strArr = ["d","cd","bcd","abcd","ab","a"]
sorted(strArr,{(s1:String,s2:String)->Bool in
if countElements(s1) != countElements(s2)
{
return countElements(s1) != countElements(s1) < countElements(s2)
}
return s1 < s2
})
使用闭包简化语法
原文链接:https://www.f2er.com/swift/327538.html