# Sorting Dates

I have an array full of birthdays but am having trouble using srtcmp to sort them.
###### Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Commented:
You can use strcmp to sort on dates if the dates are of the form YYYYMMDD .
It still works if there is delimiters between year month and day  2003/10/29 .

You could attempt to a triple-sort on year, month, and day.  However, I would
simply convert the dates to a numerical time - either time_t or simply number
of days since some baseline.

/* returns number of days since year 0 for year.
* understands leapyears, but ignores julian
* calendar changes.  this is dumb and simplistic.
*/
static int numdays (int year, int month, int day)
{
static int mdays[] = { 0, 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 };
int days;

/* as a convenience, assume a two digit year is modern */
if (year < 100)
year += (year < 10)? 2000 : 1900;

if ((month < 1) || (month > 12))
month = 1;
if ((day < 1) || (day > 31))
day = 1;

days = year*365 + year/4 - (year/100 - year/400);
days += mdays[month];
days += day;

/* add a day if current year is leap year */
if ((month > 2) && !(year % 4) && (year %100) && !(year % 400))
days++;

return days;
}

Experts Exchange Solution brought to you by