esreli
esreli

Reputation: 5061

NSSortDescriptor should sort NSDates

It appears NSSortDescriptor should be a fairly easy class to work with.

I have stored in CoreData an entity with an attribute of type NSDate appropriately named @"date". I am attempting to apply a sort descriptor to a NSFetchRequest and it doesn't appear to be returning the results I had hoped for.

The result I am hoping for is to simply arrange the dates in chronological order, and instead they are returned in the order for which they were added to CoreData.

Perhaps you can offer guidance?

Is the parameter 'ascending' what controls the sort?

Some Code:

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entityDesc = [NSEntityDescription entityForName:@"Data" inManagedObjectContext:self.managedObjectContext];
[fetchRequest setEntity:entityDesc];

[fetchRequest setFetchBatchSize:10];

NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"date" ascending:YES];
NSArray *sortDescriptors = [NSArray arrayWithObjects:sortDescriptor, nil];    

[fetchRequest setSortDescriptors:sortDescriptors];

NSArray *fetchedObjects = [self.managedObjectContext executeFetchRequest:fetchRequest error:&error];

Upvotes: 4

Views: 11212

Answers (4)

Naman Vaishnav
Naman Vaishnav

Reputation: 1089

Add timestamp - String type field in CoreData entity

creationDate

during insertion of the record

  NSManagedObject *newCredetial = [NSEntityDescription insertNewObjectForEntityForName:@"Name" inManagedObjectContext:context];[newCredetial setValue:[self getDate] forKey:@"creationDate"];

date function

- (NSString *)getDate{
NSDate *date = [NSDate date];
NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
formatter.dateFormat = @"ddMMyyyyhhmmssss";

return [formatter stringFromDate:date];}

and Use Short Description during Fetching module

NSManagedObjectContext *managedObjectContext = [self managedObjectContext];
NSFetchRequest *fetchRequest1 = [[NSFetchRequest alloc] initWithEntityName:@"Name"];

NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"creationDate" ascending:NO];
[fetchRequest1 setSortDescriptors:@[sortDescriptor]];


arrNameInfo = [[managedObjectContext executeFetchRequest:fetchRequest1 error:nil] mutableCopy];

Upvotes: 0

user2339385
user2339385

Reputation: 17

just change samle ""date to Date

like in

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

change to

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

and it works perfectly

Upvotes: -1

Mundi
Mundi

Reputation: 80265

Separate sort routines (functions or blocks) look like overkill to me. I am routinely sorting Core Data entities by date attribtues just with the sort descriptor method you show in your code. It works perfectly every time.

Clearly, there must be some other mistake.

  • Check if the type is set correcly in your managed object model editor, and in the class file (Data.h, in your case).
  • Also, check if you are not manipulating this attribute so that the creation order ensues.
  • Also, make sure you are not mixing up this attribute with another attribute of type NSDate.

Upvotes: 3

Jaybit
Jaybit

Reputation: 1864

Here is more information on NSSortDescriptors

Or if you want to sort an array of dates after getting them for coreDate:

//This is how I sort an array of dates.
NSArray *sortedDateArray = [dateArray sortedArrayUsingFunction:dateSort context:NULL];

// This is a sorting function
int dateSort(id date1, id date2, void *context)
{    
    return [date1 compare:date2];
}

Here is code straight from apple for sorting integers (just modify to sort dates):NSComparator

NSArray *sortedArray = [array sortedArrayUsingComparator: ^(id obj1, id obj2) {

    if ([obj1 integerValue] > [obj2 integerValue]) {
        return (NSComparisonResult)NSOrderedDescending;
    }

    if ([obj1 integerValue] < [obj2 integerValue]) {
        return (NSComparisonResult)NSOrderedAscending;
    }
    return (NSComparisonResult)NSOrderedSame;
}];

Upvotes: 3

Related Questions