NSSortDescriptor sorting using NSDate in Swift
Asked Answered
P

1

20

How would I sort a NSFetchRequest with that the date property of the managed object. So that it creates a array with the dates going in order?

Here is my code so far...

    var request : NSFetchRequest = NSFetchRequest(entityName: "History");
    request.predicate = NSPredicate(format: "counts = %@", true)
    request.sortDescriptors = [???] <- What should I put here?
    var results : [NSManagedObject] = context.executeFetchRequest(request, error: nil) as [NSManagedObject]
Psychosis answered 11/10, 2014 at 14:23 Comment(0)
S
37
// newest first
request.sortDescriptors = [NSSortDescriptor(key: "date", ascending: false)]

NB: You do not need : [NSManagedObject] if you already have as [NSManagedObject].
You do not need : NSFetchRequest when you use the factory method of NSFetchRequest.

Selfheal answered 11/10, 2014 at 14:26 Comment(1)
I see those NBs, but I am just so used to having the type nearby identifier declaration. I'm sure I'll get used to it.Lepore

© 2022 - 2024 — McMap. All rights reserved.