我的实体有NSManagedObject
我自己的班级(BPMonitor
) - BPMonitor
:
import UIKit
import CoreData
@objc(BPMonitor)
class BPMonitor: NSManagedObject {
@NSManaged var sisPress: String
@NSManaged var diaPress: String
@NSManaged var hbPress: String
@NSManaged var datePress: NSDate
}
在代码中,我从Core Data接收数据到数组:
var results:[BPMonitor]=[]
...
...
我如何按字段datePress
(NSDate
)按降序对数组进行排序?
答案 0 :(得分:11)
您只需使用Swift提供的标准sort
或sorted
函数:
var results: [BPMonitor] = []
var sortedResults = sorted(results, {
$0.datePress.compare($1.datePress) == NSComparisonResult.OrderedDescending
})