简体   繁体   中英

iPhone - sorting the results of a core data entity

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

Xy

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.

The order I need is ascending as they were real float numbers.

How do I do that?

thanks.

You can sort this by custom method. you can convert the string into float value then you can easily sort that.

same as

NSString *a=@"123.45";
float f=[a floatValue];

-(NSMutableArray *)sortByfloatvalue:(NSMutableArray *)array
{

   for(int i=0;i<[array count];i++)
    {
        for(int j=i+1;j<[array count];j++)
          {
              if([[array objectAtIndex:i] floatValue]>[array objectAtIndex:j])
               {
                 [array exchangeObjectAtIndex:i withObjectAtIndex:j];
               }
           }
     }
     return array;
}

You have two options:

  1. change the ProductNumber to a float type in the schema and your existing code in the FRC would sort like you want
  2. add another property that mirrors the ProductNumber but is a float like ProductNumberFloat and then point your FRC to that property for sorting

Here is code for #2 in your custom class for your Attributes entity for keeping the float value consistant:

- (void)setProductNumber:(NSString *)newProductNumber
{
    [self willChangeValueForKey:@"ProductNumber"];
    [self setPrimitiveValue:newProductNumber forKey:@"ProductNumber"];
    [self didChangeValueForKey:@"ProductNumber"];
    // set the mirror value
    [self setValue:[NSNumber numberWithFloat:[newProductNumber floatValue]] forKey:@"ProductNumberFloat"];
}

Dont forget to migrate your current data over to this new property.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM