如何按成员NSDate排序(升序)结构数组?
struct TimelineItem {
var creationDate:NSDate?
var itemImage:UIImage?
var itemDescription:NSAttributedString?
var itemType:timelineType
var photoURLStrings:[String]?
var videoURLStrings:[String]?
init(creationDate:NSDate, itemImage:UIImage, itemDescription:NSAttributedString, itemType:timelineType) {
self.creationDate = creationDate
self.itemImage = itemImage
self.itemDescription = itemDescription
self.itemType = itemType
}
}
var gTimelineItems:[TimelineItem] = []
我想做的事情如下:
gTimelineItems.sort { $1.creationDate! > $1.creationDate! }
但我得到二元运算符'>'的错误不能应用于两个NSDate操作数。
答案 0 :(得分:1)
NSDate
未实施Comparable
协议。试试这个:
gTimelineItems.sort { $0.creationDate!.timeIntervalSince1970 > $1.creationDate!.timeIntervalSince1970 }
答案 1 :(得分:0)
感谢Zoff,我使用了修改过的' sortInPlace':
gTimelineItems.sortInPlace { $0.creationDate!.timeIntervalSince1970 > $1.creationDate!.timeIntervalSince1970 }