Reputation: 1805
I have some string saved in core data
@"123", @"1" , @"432", @"90003", @"4567", @"1002"
This needs to be sorted ascending and this is the code i have written
NSFetchRequest *request1 = [[NSFetchRequest alloc] init];
[request1 setEntity:[NSEntityDescription entityForName:@"ABCD" inManagedObjectContext:context]];
[request1 setIncludesPendingChanges:NO];
sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"statusIdNo" ascending:YES selector:@selector(localizedStandardCompare:)];
[request1 setSortDescriptors:[NSArray arrayWithObject:sortDescriptor]];
statusArray = [context executeFetchRequest:request1 error:nil];
But this code is not sorting it the way required. Am i missing anything here?
EDIT:
The Required Result : @"1" , @"123", @"432", , @"1002", @"4567", @"90003"
Whats coming now : @"123"@"432",@"1" , @"1002", @"90003", @"4567"
One more thing i would like to add is that, the array doesn’t contain the strings as it is. These are core data entity objects. Like example it contains students object say for class X which has all the students’ roll numbers, names and marks as their attributes. And we need to sort it based on roll numbers which is stored as an NSNumber.
Upvotes: 2
Views: 1069
Reputation: 481
In your case use NSSortDescriptor
like this:
[NSSortDescriptor sortDescriptorWithKey:@"statusIdNo"
ascending:YES
selector:@selector(localizedStandardCompare:)];
more information about this comparison is here: https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/SortDescriptors/Articles/Creating.html
EDIT:
about setIncludesPendingChanges
from https://developer.apple.com/library/mac/documentation/cocoa/reference/CoreDataFramework/Classes/NSFetchRequest_Class/NSFetchRequest.html#jumpTo_29
A value of YES is not supported in conjunction with the result type NSDictionaryResultType, including calculation of aggregate results (such as max and min). For dictionaries, the array returned from the fetch reflects the current state in the persistent store, and does not take into account any pending changes, insertions, or deletions in the context.
If you need to take pending changes into account for some simple aggregations like max and min, you can instead use a normal fetch request, sorted on the attribute you want, with a fetch limit of 1.
EDIT 2:
my code:
- (void) getAllObjects
{
NSManagedObjectContext *context;
context = [(AppDelegate *)[[UIApplication sharedApplication] delegate] managedObjectContext];
NSFetchRequest *request1 = [[NSFetchRequest alloc] init];
[request1 setEntity:[NSEntityDescription entityForName:@"Entity" inManagedObjectContext:context]];
[request1 setIncludesPendingChanges:NO];
NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"str" ascending:YES selector:@selector(localizedStandardCompare:)];
[request1 setSortDescriptors:[NSArray arrayWithObject:sortDescriptor]];
NSArray *statusArray = [context executeFetchRequest:request1 error:nil];
for (Entity *ent in statusArray)
{
NSLog(@"%@", ent.str);
}
}
and result:
2014-04-11 20:06:34.454 smth1[8918:60b] 1
2014-04-11 20:06:34.456 smth1[8918:60b] 123
2014-04-11 20:06:34.456 smth1[8918:60b] 432
2014-04-11 20:06:34.457 smth1[8918:60b] 1002
2014-04-11 20:06:34.457 smth1[8918:60b] 4567
2014-04-11 20:06:34.458 smth1[8918:60b] 90003
Upvotes: 3