Swift how to sort array of custom objects by property value

前端 未结 18 2167
逝去的感伤
逝去的感伤 2020-11-22 02:12

lets say we have a custom class named imageFile and this class contains two properties.

class imageFile  {
    var fileName = String()
    var fileID = Int(         


        
18条回答
  •  情话喂你
    2020-11-22 02:31

    You return a sorted array from the fileID property by following way:

    Swift 2

    let sortedArray = images.sorted({ $0.fileID > $1.fileID })
    

    Swift 3 OR 4

    let sortedArray = images.sorted(by: { $0.fileID > $1.fileID })
    

    Swift 5.0

    let sortedArray = images.sorted {
        $0.fileID < $1.fileID
    }
    

提交回复
热议问题