Làm thế nào để sắp xếp mảng các đối tượng tùy chỉnh theo giá trị thuộc tính trong Swift?

{{FormatNumbertoThousand(model.total_like)}} lượt thích
102 lượt xem
Swift middle

Để sort tại chỗ, hãy xem xét:

var images : [imageFile] = []
images.sorted(by: { $0.fileID > $1.fileID }) // swift 3+
images.sorted { $0.fileID < $1.fileID } // exploiting a trailing closure

Nếu bạn định làm điều này nhiều lần và muốn định nghĩa một hàm:

func sorterForFileIDASC(this:imageFile, that:imageFile) -> Bool {
   return this.fileID > that.fileID
}
...
images.sort(by: sorterForFileIDASC)
{{login.error}}