Solved

Sorting Dates

Posted on 2003-10-30
4
138 Views
Last Modified: 2010-04-17
I have an array full of birthdays but am having trouble using srtcmp to sort them.
0
Comment
Question by:Toy_Toy
4 Comments
 
LVL 23

Accepted Solution

by:
brettmjohnson earned 75 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
C# Error - Add Failed 12 99
Programatically extract date from website 8 83
AvlTree-Node Data type 4 22
Automate and generate Azure reports for the following items 3 40
If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

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

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

Join & Ask a Question