Swift how to sort array of custom objects by property value

前端 未结 18 2168
逝去的感伤
逝去的感伤 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:33

    Two alternatives

    1) Ordering the original array with sortInPlace

    self.assignments.sortInPlace({ $0.order < $1.order })
    self.printAssignments(assignments)
    

    2) Using an alternative array to store the ordered array

    var assignmentsO = [Assignment] ()
    assignmentsO = self.assignments.sort({ $0.order < $1.order })
    self.printAssignments(assignmentsO)
    

提交回复
热议问题