(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?
Summary: in this tutorial, you will learn how to sort an array of objects by the values of the object’s properties. To sort an array of objects, you use the sort () method and provide a comparison function that determines the order of objects. Suppose that you have an array of employee objects as follows:
In the main () method, we've created an array list of custom objects list, initialized with 5 objects. For sorting the list with the given property, we use list 's sort () method. The sort () method takes the list to be sorted (final sorted list is also the same) and a comparator.
We can sort and filter data on various conditions, and sorting by the value of a string property is a common one. For example, suppose we have a list of students with their firstName and lastName: We could sort these students by either their firstName or lastName.
For sorting the list with the given property, we use list 's sort () method. The sort () method takes the list to be sorted (final sorted list is also the same) and a comparator. and finally returns positive number if o1's property is greater than o2's, negative if o1's property is lesser than o2's, and zero if they are equal.
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.
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];
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With