Swift排序Sort函數用法

來源:互聯網
上載者:User

標籤:sort   swift   ios   

簡書地址:http://www.jianshu.com/p/ad71c94e7bc6

摘自stackoverflow的問答用了幾分鐘做的簡單翻譯一個例子

直接貼代碼,不過多解釋

//這是我們的modelclass imageFile  {    var fileName = String()    var fileID = Int()}
//使用var images : [imageFile] = []images.sort({ $0.fileID > $1.fileID })
下面是閉包的進階使用
// 一般的代碼形式images.sort({ (image1: imageFile, image2: imageFile) -> Bool in return image1.fileID > image2.fileID })// 省略(->)的形式images.sort({ image1, image2 in return image1.fileID > image2.fileID })// 帶傳回值的簡單閉包形式images.sort({ image1, image2 in image1.fileID > image2.fileID })// 隱含參數的形式images.sort({ $0.fileID > $1.fileID })// 以下結果都是相同,只是書寫形式不同images = images.sorted({ (image1: imageFile, image2: imageFile) -> Bool in return image1.fileID > image2.fileID })images = images.sorted({ image1, image2 in return image1.fileID > image2.fileID })images = images.sorted({ image1, image2 in image1.fileID > image2.fileID })images = images.sorted({ $0.fileID > $1.fileID })
//swift標準庫sort(&images, { (image1: imageFile, image2: imageFile) -> Bool in return image1.fileID > image2.fileID })sort(&images, { image1, image2 in return image1.fileID > image2.fileID })sort(&images, { image1, image2 in image1.fileID > image2.fileID })sort(&images, { $0.fileID > $1.fileID })//使用方法images = sorted(images, { (image1: imageFile, image2: imageFile) -> Bool in return image1.fileID > image2.fileID })images = sorted(images, { image1, image2 in return image1.fileID > image2.fileID })images = sorted(images, { image1, image2 in image1.fileID > image2.fileID })images = sorted(images, { $0.fileID > $1.fileID })

Swift排序Sort函數用法

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.