Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
Solved

# Sorting Dates

Posted on 2003-10-30
Medium Priority
143 Views
I have an array full of birthdays but am having trouble using srtcmp to sort them.
0
Question by:Toy_Toy
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• Learn & ask questions

LVL 23

Accepted Solution

brettmjohnson earned 300 total points
ID: 9655414
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;
}
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
We live in a world of interfaces like the one in the title picture. VBA also allows to use interfaces which offers a lot of possibilities. This article describes how to use interfaces in VBA and how to work around their bugs.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
Starting up a Project
###### Suggested Courses
Course of the Month8 days, 22 hours left to enroll

#### 604 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.