iPhone - sorting the results of a core data entity

Posted by Digital Robot on Stack Overflow See other posts from Stack Overflow or by Digital Robot
Published on 2010-12-29T05:45:49Z Indexed on 2010/12/29 5:54 UTC
Read the original article Hit count: 195

Filed under:
|

I have a core data entity that represents the attributes of a product, as number, price, etc. The product number is a NSString property and follows the form

X.y

where X is a number variable of digits and Y is one digit. For example. 132.2, 99.4, etc.

I am querying the database to obtain the list of product numbers in order:

The code is like this:

+ (NSArray*)todosOsItens:(NSString *)pName inManagedObjectContext:(NSManagedObjectContext *)context
{

 Product *aProduct = [Product productWithName:pName inManagedObjectContext:context];

 NSArray *all = nil;

 NSFetchRequest *request = [[NSFetchRequest alloc] init];

 request.entity = [NSEntityDescription entityForName:@"Attributes" inManagedObjectContext:context];
 request.predicate = [NSPredicate predicateWithFormat:
       @"(belongsTo == %@)", aProduct];

 [request setResultType:NSDictionaryResultType];
 [request setReturnsDistinctResults:YES];
 [request setPropertiesToFetch:[NSArray arrayWithObject:item]];


 NSSortDescriptor *sortByItem =  [NSSortDescriptor sortDescriptorWithKey:@"ProductNumber" ascending:YES];
 NSArray *sortDescriptors = [NSArray arrayWithObject:sortByItem];
 [request setSortDescriptors:sortDescriptors];

 NSError *error = nil;
 all = [[context executeFetchRequest:request error:&error] mutableCopy];
 [request release];

 return all;
}

but this query is not returning the sorted results. The results are coming on their natural order on the database.

How do I do that?

thanks.

© Stack Overflow or respective owner

Related posts about iphone

Related posts about core-data