Reputation: 5157
I have a core data entity called "TruckNumber" which has a string as it's only property. The string is usually a 1-3 digit integer (as a string) but sometimes can have letters such as TMP9. The name of the property is "itsNotANumma". I am doing a fetch request to populate a picker, but they are not being sorted and I don't know why. I've used this exact technique on other entities for other pickers and never had a problem. Please help... Here's the relevant code:
// Fetch truck numbers
NSFetchRequest *truckNumberFetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *truckEntity = [NSEntityDescription entityForName:@"TruckNumber" inManagedObjectContext:self.managedObjectContext];
[truckNumberFetchRequest setEntity:truckEntity];
// Sort Descriptor
NSSortDescriptor *truckDescriptor = [[NSSortDescriptor alloc] initWithKey:@"itsNotANumma" ascending:YES];
NSArray *truckSortDescriptors = [[NSArray alloc] initWithObjects:truckDescriptor, nil];
[inventoryFetchRequest setSortDescriptors:truckSortDescriptors];
error = nil;
NSArray *truckResults = [managedObjectContext executeFetchRequest:truckNumberFetchRequest error:&error];
if (error)
NSLog(@"Unresolved error while saving context: %@, %@", error, [error userInfo]);
truckNumbersArray = [[NSMutableArray alloc] init];
for (TruckNumber *truckNumber in truckResults)
{
[truckNumbersArray addObject:truckNumber.itsNotANumma];
}
Here is my data model for truck number:
These are the results:
Upvotes: 0
Views: 657
Reputation: 31
[NSSortDescriptor sortDescriptorWithKey:@"self" ascending:YES comparator:^(id obj1, id obj2){
return [(NSString*)obj1 compare:(NSString*)obj2 options:NSNumericSearch];
}];
It works so perfectly if you have numbers in a range from 1-5000 or onwards.
Upvotes: 0
Reputation: 34912
Looks like you have a simple typo:
[inventoryFetchRequest setSortDescriptors:truckSortDescriptors];
Should be:
[truckNumberFetchRequest setSortDescriptors:truckSortDescriptors];
I would assume anyway, from reading that code.
Upvotes: 2