cannyboy
cannyboy

Reputation: 24426

Sorting array of custom objects using the value of an internal object's instance variable

(sorry about the long title)

I have a custom object Person, which in turn has an NSSet which has several custom objects called Appointment. A Person therefore can have several appointments. Appointment has the values startTime and endTime.

These are Core Data NSMangagedObject classes.

@interface Person : NSManagedObject

@property (nonatomic, retain) NSString *personName;
@property (nonatomic, retain) NSSet *appointments;

// etc

@end


@interface Appointment : NSManagedObject

@property (nonatomic, retain) NSNumber * startSecond;
@property (nonatomic, retain) NSNumber * endSecond;

// etc

@end

How would I get a list of Persons, in order of the earliest startSecond within any of their appointments?

Upvotes: 11

Views: 10306

Answers (3)

Ell Neal
Ell Neal

Reputation: 6064

You can use sort descriptors and KVC collection operators:

NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"[email protected]" ascending:YES];

For example, in a CoreData fetch:

NSFetchRequest *request = [[NSFetchRequest alloc] initWithEntityName:@"Person"];

NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"[email protected]" ascending:YES];
[request setSortDescriptors:@[sortDescriptor]];

NSError *error = nil;
NSArray *sortedResults = [context executeFetchRequest:request error:&error];

Or just sorting an array:

NSArray *people = @[...];
NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"[email protected]" ascending:YES];

NSArray *sortedPeople = [people sortedArrayUsingDescriptors:@[sortDescriptor]];

More information on KVC collection operators can be found in the KVC Programming Guide.

Upvotes: 38

Alex
Alex

Reputation: 5210

If you have the data in an NSArray form you can sort it like this:

NSArray *sortedPersonArray = [coreDataPersonArray sortedArrayUsingSelector:@selector(compare:)];

- (NSComparisonResult)compare:(Person *)personObject {
    return [self.startSecond compare:personObject.startSecond];
}

Upvotes: 2

onekiloparsec
onekiloparsec

Reputation: 2063

A suggestion:

// Sorting key
NSString *key = @"startSecond";

// A mutable array version of your list of Persons.
NSMutableArray *a = [NSMutableArray arrayWithObjects:Person1, Person2, Person3, nil];

// Then use the sorted appointements to get your sorted person array.
[a sortUsingComparator:^NSComparisonResult(Person *p1, Person *p2) {
    NSSortDescriptor *sortDesc1 = [NSSortDescriptor sortDescriptorWithKey:key ascending:NO];
    NSArray *sortedApp1 = [p1.appointements sortedArrayUsingDescriptors:[NSArray arrayWithObject:sortDesc1]];

    NSSortDescriptor *sortDesc2 = [NSSortDescriptor sortDescriptorWithKey:key ascending:NO];
    NSArray *sortedApp2 = [p2.appointements sortedArrayUsingDescriptors:[NSArray arrayWithObject:sortDesc2]];

    return [[[sortedApp1 objectAtIndex:0] valueForKey:key] compare:[[sortedApp2 objectAtIndex:0] valueForKey:key]];
}

Upvotes: 1

Related Questions