Sort array of objects by their NSDate property [duplicate]
Thanks for all the answers, I came across this answer that solved my problem.Check NSGod's answer
Here is the code thanks to user: NSGod:
NSSortDescriptor *dateDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"startDateTime" ascending:YES];NSArray *sortDescriptors = [NSArray arrayWithObject:dateDescriptor];NSArray *sortedEventArray = [nodeEventArray sortedArrayUsingDescriptors:sortDescriptors];
NSSortDescriptor* sortByDate = [NSSortDescriptor sortDescriptorWithKey:@"nsdatepropertyname" ascending:YES]; [mutableArray sortUsingDescriptors:[NSArray arrayWithObject:sortByDate]];
You can use:
NSArray *sortedArray = [mutableArray sortedArrayUsingComparator:^NSComparisonResult(id a, id b) { NSDate *first = [(Person*)a birthDate]; NSDate *second = [(Person*)b birthDate]; return [first compare:second]; }];
Or see this link