date sorting code

i was making a birthday-tracking application, and wanted to be able to do year-insensitive date sorts. i couldn’t find what I was looking for, so I wrote this:

int personSortByBirthday (NSCalendarDate *d1, NSCalendarDate *d2, void *context) {
	int day1 = [d1 dayOfMonth];
	int day2 = [d2 dayOfMonth];
	int month1 = [d1 monthOfYear];
	int month2 = [d2 monthOfYear];
	
	if (month1 < month2) {
		return NSOrderedAscending;
	} else if (month1 > month2) {
		return NSOrderedDescending;
	} else {
		if (day1 < day2) {
			return NSOrderedAscending;
		} else if (day1 > day2) {
			return NSOrderedDescending;
		} else {
			return NSOrderedSame;
		}
	}       
}

One Response to date sorting code

  1. Pingback: blog.shrub » Blog Archive » My favourite Tiger feature

Leave a Reply

Your email address will not be published. Required fields are marked *