0

Right now I have a tableview that loads data from coredata and I would like to know how it is possible to order these cells by their order of creation. I'd like for the user to be able to see the most recent cells created at the top of the tableview.

Thanks.

Leighton
  • 6,559
  • 7
  • 21
  • 28
  • Sort the array of results based on a date property. [Here's how.][1] [1]: http://stackoverflow.com/a/6256848/1228075 – Armin Jan 25 '15 at 01:17
  • 1
    Don't think of it as "the most recent cells created" but rather the most recent data object created – Ian Jan 25 '15 at 01:41
  • Ok, so how would I sort by the most recent data object created? – Leighton Jan 27 '15 at 22:50

1 Answers1

0

You can:

  1. Add a sort descriptor to your request:

    //Entity
    NSString *entityName = @"Days";
    
    //Fetch Request
    NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:entityName];
    
    //Sort
    request.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"created_at" ascending:YES]];
    
  2. Sort the array of results:

    NSSortDescriptor * sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"DatePropertyName" ascending:YES];
    NSArray *descriptors = [NSArray arrayWithObject:sortDescriptor]; 
    NSArray *sortedArray = [myArray sortedArrayUsingDescriptors:descriptors];
    
Armin
  • 1,150
  • 8
  • 24