Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 843
cannyboy Avatar asked Jan 30 '12 10:01

cannyboy


People also ask

How to sort an array of objects by the object’s properties?

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:

How to sort a list of custom objects in Java?

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.

Can We sort data by the value of a string property?

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.

How to sort the list with the given property in Java?

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.


2 Answers

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.

like image 88
Ell Neal Avatar answered Sep 19 '22 18:09

Ell Neal


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];
}
like image 22
Alex Stanciu Avatar answered Sep 20 '22 18:09

Alex Stanciu