Like stated here, you can get the date components of an NSDate and sort by that value.
NSDateComponents *components = [[NSCalendar currentCalendar] components:NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear fromDate:[NSDate date]];
NSInteger day = [components day];
NSInteger month = [components month];
NSInteger year = [components year];
You can ignore any values you would like.
EDIT: Just combining the two answers given. Credit to driis for the sorting code.
NSArray* sortedArray = [yourArray sortedArrayUsingComparator:^NSComparisonResult(NSDate* lhs, NSDate* rhs)
{
NSDateComponents *lscomponents = [[NSCalendar currentCalendar] components:NSCalendarUnitDay | NSCalendarUnitMonth fromDate:lhs];
NSDateComponents *rhcomponents = [[NSCalendar currentCalendar] components:NSCalendarUnitDay | NSCalendarUnitMonth fromDate:rhs];
NSDateComponents *currentcomponents = [[NSCalendar currentCalendar] components:NSCalendarUnitDay | NSCalendarUnitMonth fromDate:[NSDate date]];
NSInteger lday = [lscomponents day];
NSInteger lmonth = [lscomponents month];
NSInteger rday = [rhcomponents day];
NSInteger rmonth = [rhcomponents month];
if(lmonth < [currentcomponents month])
lmonth += 12;
else if(lmonth == [currentcomponents month])
{
if(lday < [currentcomponents day])
lmonth += 12;
}
if(rmonth < [currentcomponents month])
rmonth += 12;
else if(rmonth == [currentcomponents month])
{
if(rday < [currentcomponents day])
rmonth += 12;
}
int diff = lmonth - rmonth;
if (diff == 0)
diff = lday - rday;
if (diff < 0)
return NSOrderedDescending;
if (diff > 0)
return NSOrderedAscending;
return NSOrderedSame;
}];
You can make this much faster by cacheing the calendar.
EDIT2:
I have updated my sorting to take into account your custom need. If the month and day is before right now you add 12 to the month. Then compare based on the new pseudo-month. That should give your custom sorting around today.