Solved

Sorting Dates

Posted on 2003-10-30
4
135 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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
sumHeights  challenge 17 61
countPairs challenge 7 58
Math question 3 60
index Out OF Range Exception error 4 33
Article by: Nadia
Linear search (searching each index in an array one by one) works almost everywhere but it is not optimal in many cases. Let's assume, we have a book which has 42949672960 pages. We also have a table of contents. Now we want to read the content on p…
A short article about a problem I had getting the GPS LocationListener working.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

743 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now