nick shmick
nick shmick

Reputation: 925

how to fetch result from core data sorted by the object creation date?

sorry if the title is not clear, i'll explain:

I have a note creation page, after creating a note I'm saving the note using the model class (nsmanagedobject sub class).

I have another table view controller which I'm showing the notes, and I want the notes on the table view to be sorted by creation date, is there a key for that?

this is what i have now:

NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"body" ascending:YES];

the key here is the text of the note so its sorted by the abc, can you help me to sort it by date?

tnx

Upvotes: 1

Views: 1986

Answers (1)

Paulw11
Paulw11

Reputation: 114773

There is no implicit creation date attribute for a managed object. You can create an attribute, say creationDate, in your model and set it to [NSDate date] when you create an object.

Then you can simply sort on this attribute

 NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"creationDate" ascending:YES];

Upvotes: 5

Related Questions