toiavalle
toiavalle

Reputation: 434

Swift sort core data by date (not just time)

I'm building a app in Swift using core data. I'm using task Entities with due attribute that is a Date. I need to sort the tasks according to this due date.

I tried using a NSSortDescriptor with key due

NSSortDescriptor.init(key: "due", ascending: false)

Issue:

It sorts my data by time only - not considering the day/month/year

here is the data I got from my fetch request:

Optional(2017-02-06 03:29:18 +0000)
Optional(2017-02-08 03:29:27 +0000)
Optional(2017-02-07 03:29:38 +0000)
Optional(2017-02-08 03:30:29 +0000)

How can I sort it by what comes sooner? (considering the whole NSDate not just the time portion)

I want it to be:

Optional(2017-02-06 03:29:18 +0000)
Optional(2017-02-07 03:29:38 +0000)
Optional(2017-02-08 03:29:27 +0000)
Optional(2017-02-08 03:30:29 +0000)

Complete Code All tasks completed are 0 in the list and I'm calling:

helper(showComp: true, sortBy: NSSortDescriptor.init(key: "due", ascending: false))

helper func:

func helper(showComp: Bool, sortBy: NSSortDescriptor?){
let g = cur as! Group
guard let appDelegate = UIApplication.shared.delegate as? AppDelegate else {
    return
}

let managedContext = appDelegate.persistentContainer.viewContext

let fetchRequest = NSFetchRequest<NSManagedObject>(entityName: "Task")
let sortDescriptor0 =  NSSortDescriptor.init(key: "completed", ascending: true)
let sortDescriptor =  NSSortDescriptor.init(key: "index", ascending: true)
if sortBy == nil {
    fetchRequest.sortDescriptors = [sortDescriptor0, sortDescriptor]
}else{
    fetchRequest.sortDescriptors = [sortDescriptor0, sortBy!]
}
if (cur as! Group).name != "All"{
    var predicate = NSPredicate(format: "group = %@", argumentArray: [g])
    if !showComp {
        predicate = NSPredicate(format: "group = %@ && completed = %@", argumentArray: [g, 0])
    }
    fetchRequest.predicate = predicate
}
do {
    groups = try managedContext.fetch(fetchRequest)
} catch let error as NSError {
    print("Could not fetch. \(error), \(error.userInfo)")
}

Photo of the core data attribute enter image description here

Upvotes: 1

Views: 2777

Answers (1)

mqz.kim
mqz.kim

Reputation: 1039

Swift 3

var sdSortDate = NSSortDescriptor.init(key: "date", ascending: false)
fr.sortDescriptors = [sdSortDate]
let result4 = try! context.fetch(fr)

I tested it in Swift 3, works well

[<NSManagedObject: 0x618000089060> (entity: CoreDate; id: 0x61800002c460 <x-coredata://E7D9FD6F-5DCF-464B-93BF-D1609BF35695/CoreDate/p3> ; data: {
    date = "2017-02-09 21:35:19 +0000";
}), <NSManagedObject: 0x61800008b680> (entity: CoreDate; id: 0x60000002c140 <x-coredata://E7D9FD6F-5DCF-464B-93BF-D1609BF35695/CoreDate/p1> ; data: {
    date = "2017-02-06 04:41:59 +0000";
}), <NSManagedObject: 0x60800008c080> (entity: CoreDate; id: 0x60800002d460 <x-coredata://E7D9FD6F-5DCF-464B-93BF-D1609BF35695/CoreDate/p2> ; data: {
    date = "2017-02-17 18:28:39 +0000";
})]

After sort

[<NSManagedObject: 0x61800008b680> (entity: CoreDate; id: 0x60000002c140 <x-coredata://E7D9FD6F-5DCF-464B-93BF-D1609BF35695/CoreDate/p1> ; data: {
    date = "2017-02-06 04:41:59 +0000";
}), <NSManagedObject: 0x618000089060> (entity: CoreDate; id: 0x61800002c460 <x-coredata://E7D9FD6F-5DCF-464B-93BF-D1609BF35695/CoreDate/p3> ; data: {
    date = "2017-02-09 21:35:19 +0000";
}), <NSManagedObject: 0x60800008c080> (entity: CoreDate; id: 0x60800002d460 <x-coredata://E7D9FD6F-5DCF-464B-93BF-D1609BF35695/CoreDate/p2> ; data: {
    date = "2017-02-17 18:28:39 +0000";
})]

Upvotes: 3

Related Questions