lets say we have a custom class named imageFile and this class contains two properties.
class imageFile {
var fileName = String()
var fileID = Int()
}
lots of them stored in Array
var images : Array = []
var aImage = imageFile()
aImage.fileName = "image1.png"
aImage.fileID = 101
images.append(aImage)
aImage = imageFile()
aImage.fileName = "image1.png"
aImage.fileID = 202
images.append(aImage)
question is: how can i sort images array by 'fileID' ASC or DESC?
Nearly everyone gives how directly, let me show the evolvement:
you can use the instance methods of Array:
For elaborate explanation about the working principle of sort, see The Sorted Function.
In Swift 3.0
I do it like this and it works:
var images = [imageFile]() images.sorted(by: {$0.fileID.compare($1.fileID) == .orderedAscending })