Swift how to sort array of custom objects by property value

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

    If you want to sort original array of custom objects. Here is another way to do so in Swift 2.1

    var myCustomerArray = [Customer]()
    myCustomerArray.sortInPlace {(customer1:Customer, customer2:Customer) -> Bool in
        customer1.id < customer2.id
    }
    

    Where id is an Integer. You can use the same < operator for String properties as well.

    You can learn more about its use by looking at an example here: Swift2: Nearby Customers

提交回复
热议问题