Swift how to sort array of custom objects by property value

前端 未结 18 2181
逝去的感伤
逝去的感伤 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条回答
  •  闹比i
    闹比i (楼主)
    2020-11-22 02:50

    First, declare your Array as a typed array so that you can call methods when you iterate:

    var images : [imageFile] = []
    

    Then you can simply do:

    Swift 2

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

    Swift 3+

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

    The example above gives desc sort order

提交回复
热议问题