Code:
//define an array and use closures to sort themLet names = ["Chris","Alex","Ewa","Barry","Daniella"]//full FormNames.sort ({(s1:string, s2:string), Boolinch returnS1 >S2})//trailing closure form//a trailing closure is a closure expression that is written after the function bracket, and the function supports calling it as the last argumentNames.sort () {(s1:string, s2:string), Boolinch returnS1 >S2}//if the parameter type of the closure expression is known, you can omit theNames.sort () {(S1, S2), Boolinch returnS1 >S2}//if the return type of the closure expression is known, you can omit theNames.sort () {(S1, S2)inch returnS1 >S2}//parentheses that omit parametersNames.sort () {s1, s2inch returnS1 >S2}//the closure of a single-line expression can implicitly return the result of a single-line expression by hiding the return keywordNames.sort () {s1, s2inchS1>S2}//Swift automatically provides parameter name abbreviations for inline functions, and you can call the parameters of closures directly from $0,$1,$2Names.sort () {$0> $1}//If a function needs only one argument for a closure expression, you can omit the parentheses when you use a trailing closureNames.sort {$0> $1}//operator FunctionsNames.sort (>)
Swift Closures and simplification