3

I have a NSManagedObject my own class (BPMonitor) for my Entity - 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 receive data from Core Data to array:

var results:[BPMonitor]=[]
...
...

How I can sort my array for field datePress (NSDate) in descending order?

1 Answer 1

11

You just need to use standard sort or sorted functions provided by Swift:

var results: [BPMonitor] = []

var sortedResults = sorted(results, {
    $0.datePress.compare($1.datePress) == NSComparisonResult.OrderedDescending
})
Sign up to request clarification or add additional context in comments.

2 Comments

Why wouldn't you use an NSSortDescriptor when fetching your Core Data entities?
It lets Core Data do the work for you, and while you can sort after you retrieve, it's just an extra operation that has to take place. An NSFetchRequest takes an array of NSSortDescriptor objects, so even if you have just one NSSortDescriptor, drop it in an array and you should be ok.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.