美文网首页
Swift : The sort method and Clos

Swift : The sort method and Clos

作者: GeorgeCharles | 来源:发表于2015-11-01 15:32 被阅读33次

        Swift standard library provide a method called sort ( _: ) ,which sort an array of a known type,base on  the output of a sorting closure that you provide.Once it completes the sorting process,the sort ( _: )  method returns a new array of the same size and type as old one,with its elements in the correct sorted order.the original array isn't modified by the sort ( _: ) method.

       The closure expression example below use sort ( _: ) to sort a array of string type in reverse alphabetical order.Here is the initial array to be sorted:

    let names = ["Chris","Alex","Ewa","Barry","Daniella"]

    swift 标准库提供  sort ( _: ) 排序方法,它可以给一个 已知类型 的array排序。排序规则基于你写的 排序 闭包。当排序完成时,sort ( _: ) 方法返回一个新数组,新数组与原来的数组有相同的大小和类型。但数组中元素排序变了。原数组不会被sort ( _: ) 方法修改。

    下面的闭包表达式使用sort ( _: )方法给 一个string类型的 数组 按 字母逆序 排序。

    let names = ["Chris","Alex","Ewa","Barry","Daniella"]

    翻译就先到这,来总结一下闭包。举个栗子

    { ( parameters ) -> Return Type in

        Statements

    }

    大括号(curly brace:卷曲的括号)里面是闭包内容, 小括号()里面是参数argument ,-> 

    后面写返回值的类型,在 in 后面开始写 闭包体语句。in 来提示 闭包中参数和返回值的定义都已完成。

    相关文章

      网友评论

          本文标题:Swift : The sort method and Clos

          本文链接:https://www.haomeiwen.com/subject/tszfhttx.html