Sorting array derived from Core Data in Swift
I have NSManagedObject
my own class ( BPMonitor
) for my object - 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
}
In code, I am getting data from Core Data into an array:
var results:[BPMonitor]=[]
...
...
How can I sort the array for field datePress
( NSDate
) in descending order?
+3
Alexey Nakhimov
source
to share
1 answer
You just need to use the standard functions sort
or those sorted
provided by Swift:
var results: [BPMonitor] = []
var sortedResults = sorted(results, {
$0.datePress.compare($1.datePress) == NSComparisonResult.OrderedDescending
})
+11
akashivskyy
source
to share