Click here to Skip to main content
15,905,967 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
You are given a list of dates as an array of strings. Write a program to
sort the dates chronologically, and print the sorted list. You can use any sorting
algorithm.
(a) The dates are provided in the EU format DD-MMM-YYYY, like 17-Jul-2019.

What I have tried:

typedef struct q3
{
    int day;
    char mas[3];
    int nom;
    
}month;

month Months[12] = {
    {31,"jan",1},{28,"feb",2},{31,"mar",3},
    {30,"apr",4},{31,"may",5},{30,"jun",6},
    {31,"jul",7},{31,"aug",8},{30,"sep",9},
    {31,"oct",10},{30,"nov",11},{31,"dec",12}
};

void sort_dates(){

}

int main(int argc, char const *argv[])
{
    char *c[]={"24 Jul 2017", "25 Jul 2017", "11 Jun 1996",
                       "01 Jan 2019", "12 Aug 2005", "01 Jan 1997"};
    int n=sizeof(c)/sizeof(c[0]);
    
    return 0;
}
Posted
Updated 6-Aug-22 8:52am
Comments
Rick York 6-Aug-22 13:02pm    
You have stated your problem. What is your question?
merano99 6-Aug-22 14:01pm    
The question might be, "How can I sort a date of the form DD-MMM-YYYY?" In fact, other questions would also be possible.

In order to sort the data you need to do convert each date into an ordered string. Since each date is in a fixed form it is a simple matter to extract the three fields. You can then use the Months array to find the number of the month. But using a string for the month number would make sorting easier. So change the month table to:
C++
month Months[12] = { // no need for the number of days
    {"jan","01"},{"feb","02"},{"mar","03"},
    {"apr","04"},{"may","05"},{"jun","06"},
    {"jul","07"},{"aug","08"},{"sep","09"},
    {"oct","10"},{"nov","11"},{"dec","12"}
};

You can now combine the values to "YYYYMMDD" which makes them sortable.
 
Share this answer
 
Comments
merano99 6-Aug-22 8:04am    
If the plan is to recode the date into a string of the form "YYYYMMDD" then what is the point of the structure?

The number of days in the month could be used for checking and would therefore be useful. The number for the month, on the other hand, would be superfluous, since it can be derived from the index.
But with the sorting suggestion above, the structure is not needed anyway.
Richard MacCutchan 6-Aug-22 8:51am    
"If the plan is to recode the date into a string of the form "YYYYMMDD" then what is the point of the structure?"
The structure is just there to simplify the creation of the Months table. However, that was in the original code, and not suggested by me.

"The number of days in the month could be used for checking and would therefore be useful."
Yes, if the dates may need to be checked for validity.

"The number for the month, on the other hand, would be superfluous, since it can be derived from the index."
Quite true, but this is obviously a school assignment at a beginner stage, so let the OP think about that later.

"But with the sorting suggestion above, the structure is not needed anyway."
Again quite possibly, but let's not confuse things by suggesting a rewrite of the entire code.
merano99 6-Aug-22 10:57am    
Ok. As always, there are many ways to solve the task. Your suggestion should help. You have my 5 for it.
Richard MacCutchan 6-Aug-22 11:30am    
And as always, my suggestions are just that: suggestions. It is the OP's choice whether they make use of them.
According to the task, all month strings start with capital letters!
month Months[12] = {{ 31,"Jan",1}, ...

According to the task, the date should be in the EU format DD-MMM-YYYY. This should also be changed accordingly.
C
char *c[] = { "24-Jul-2017", "25-Jul-2017", "11-Jun-1996", "01-Jan-2019", "12-Aug-2005", "01-Jan-1997" };
size_t n = sizeof(c) / sizeof(c[0]);

Note: Since the month is to be stored with 3 letters, you need enough space for it if you want to process it as a string in the structure.

It is recommended to write a function for the output to be able to output both the original data and the sorted data.The complete main program then looks like this:
C
puts("original:");
date_print(c, n);
date_sort(c, n);
puts("sorted:");
date_print(c, n);

To sort data you first need a function that performs the comparison. If it is allowed in C the built in function qsort() is suitable for this.
see: http://www.cplusplus.com/reference/clibrary/cstdlib/qsort/

Since the data are not well comparable in the existing form it would be perhaps better to create first in an auxiliary structure and assign there a pointer to the original data, as well as an auxiliary variable to be calculated, which can be sorted easily.

Richard had already suggested creating the string as an auxiliary variable in descending order in the form "YYYYMMDD". When I look at the already existing structure for converting the month string, it seems that you should compare and sort possibly days as numbers. In any case, the months must be converted to a sortable format such as a number so that they are sortable just like the year and day.
I suggest to calculate the number of days from each date. But you can also use the date as a string with numbers as suggested by Richard. The pointer to the original date string as well as the calculated helper variable must then each be transferred to the sorthelp auxiliary structure.
The definition of the structure could look like this:
C
typedef struct {
	char* datestr;
	unsigned long days;
} sorthelp;

When the number of days has been calculated for all date strings, you can call qsort with it.
C
// create helper-struct for sorting
sorthelp *sh = malloc(sizeof(sorthelp)*n);

for (size_t i = 0; i < n; i++) {
  // copy string pointer
  ...
  // calculate days from date
  ...
}

qsort(sh, n, sizeof(sorthelp), compare);

for (size_t i = 0; i < n; i++) {
   // copy string-pointer back
   ...
}

free(sh);

Note: Instead of the auxiliary structure, the conversion and comparison could also be done directly in the compare() function. However, since the comparison might be needed very often for sorting, the performance could suffer a lot. For performance reasons I would rather implement the comparison with a number than with a string.
 
Share this answer
 
v4
Quote:
Write a program to
sort the dates chronologically,

To sort dates, only 1 method, make the dates YYYYMMDD format. Either strings or integers
C++
IntDate= YYYY*10000+ MM*100+ DD

So you need to convert input dates to appropriate format, then sort.
After that, convert back to original format and do what is needed.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900