Swift 2.0 Sorting Array of Objects by Property

前端 未结 4 1758
隐瞒了意图╮
隐瞒了意图╮ 2020-12-14 14:43

In Swift 2.0, how would you go about sorting an array of custom objects by a property? I know in Swift 1.2, this was done using sorted() and sort(). However, these methods n

4条回答
  •  长情又很酷
    2020-12-14 15:23

    In swift 3.0

    I have taken the assumption same as the example asked in the question.

    i.e customObject is MyObject with variable myDate. The array of the MyObject is myArray.We can sort like this simply.

    myArray.sort(by: { (one: MyObject, two: MyObject) -> Bool in
        one. myDate < two. myDate
    })
    

提交回复
热议问题