Member-only story
Swift how to sort array of custom objects by property value
1 min readOct 6, 2020
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 : [imageFile] = []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)
Swift 2
images.sorted({ $0.fileID > $1.fileID })
Swift 3 & 4
images.sorted(by: { $0.fileID > $1.fileID })
The example above gives desc sort order