Jane Book Address: Http://www.jianshu.com/p/ad71c94e7bc6
An example of a simple translation made in a few minutes by StackOverflow's question and answer
Direct code, but more explanation
//这是我们的modelclass imageFile { varString() var fileID = Int()}
//使用var images : [imageFile] = []images.sort$0$1.fileID })
The following is the advanced use of closures
//General Code Form Images.sort({(Image1:imagefile, image2:imagefile), Bool in return Image1.fileid > Image2.fileid})
Omitted(-)The formImages.Sort({image1, image2 in return Image1.fileid > Image2.fileid})Simple closure form with return valueImages.Sort({image1, image2 in Image1.fileid > Image2.fileid})The form of an implied parameterImages.Sort({$0. ) FileID > $1. FileID})The following results are all the same, just different forms of writingImages=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 Standard library 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})How to useImages=Sorted(images, {(Image1:imagefile, image2:imagefile), Bool in return image1.fileid > image 2.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 sort Function usage