iossortingcore-datansentitydescription

iOS: sorting an NSArray of core data objects


I have a NSArray with core data entities called:

"Struct"

thy have an attribute called "value" that can be a double

Struct *struct = [array objectAtIndex:0];
double val = [struct value];

I have to sorting this array by this value but I don't understand what's the way to do it, because I have not a key to access to the object.

Do you have any suggests?


Solution

  • NSMutableArray *sortedArray;
    sortedArray = [array sortedArrayUsingComparator:^NSComparisonResult(id a, id b) {
        NSNumber *s1 = @([(Struct *)a value]);
        NSNumber *s2 = @([(Struct *)b value]);
        return [s1 compare:s2];
    }];