You can't sort by role weight because it is possible to have more than one role that fits.
You also can't come at it from the Role (as opposed to the Person) because you have a many-to-many between role and person.
You should re-think your design because having that many-to-many there does not make much sense. A little bit of data de-normalization, changing that many-to-many to a one-to-many and duplicating the rolename and roleweight values would solve the issue.
Update
Assuming you changed the design to:
Company --< Role >-- Person
Then the solution gets much easier:
- (NSArray*)peopleSortedByWeightInCompany:(NSString*)company inManagedObjectContext:(NSManagedObjectContext*)moc
{
NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:[NSEntityDescription entityForName:@"Role" inManagedObjectContext:moc]];
[request setPredicate:[NSPredicate predicateWithFormat:@"company.name == %@", companyName]];
NSSortDescriptor *sort = [[NSSortDescriptor alloc] initWithKey:@"weight" ascending:YES];
[request setSortDescriptors:[NSArray arrayWithObject:sort]];
NSError *error = nil;
NSArray *roles = [moc executeFetchRequest:request error:&error];
[request release], request = nil;
NSAssert2(roles != nil && error == nil, @"Error fetching roles: %@\n%@", [error localizedDescription], [error userInfo]);
NSArray *people = [roles valueForKeyPath:@"@distinctUnionOfObjects.person"];
return people;
}
You basically fetch the Role entities sorted by weight and filtered by the company name. From that array of Role entities you then use KVC to gather all of the person objects on the other end of the relationship which will retrieve them in order.