sorting dates

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • georges the man
    New Member
    • Sep 2006
    • 25

    sorting dates

    how do i sort dates in c.
    i have dates in a file in the following format:

    dd/mm/yyyy
    ie 12 aug 2006

    i want to sort the dates in the file in an assending way.

    how can i do this?
  • pukur123
    New Member
    • Sep 2006
    • 61

    #2
    Read the dates one by one and parse the line to get each field of the line in separate strings and then convert them to integers and keep on storing in an a two dimensional array and then apply the sort mechanism.

    Comment

    • georges the man
      New Member
      • Sep 2006
      • 25

      #3
      how can i do this..

      i am just a beginner in c.

      not sure how should i do it

      Comment

      • pukur123
        New Member
        • Sep 2006
        • 61

        #4
        Go through the following APIs in C.....

        fopen
        fclose
        fgetc
        fputc
        fscanf
        fprintf

        By using these functions you can retrieve the characters, words and even lines from a file.

        You have to know the how to parse a string and your string contains the character / as the delimeter. So you will get three different strings out of this.

        Convert the strings to integers by using the function atoi().

        And proceed.

        Comment

        • georges the man
          New Member
          • Sep 2006
          • 25

          #5
          can u give me an example;

          say that the dates are:
          12 aug 2006
          13 aug 2006
          11 aug 2006
          09 aug 2006

          Comment

          • pukur123
            New Member
            • Sep 2006
            • 61

            #6
            In the first post you siad that you have the data in the dd/mm/yyyy format. Exacly in what format the dates are there in the file. And in the next 6 hrs you will get the code.

            Comment

            • georges the man
              New Member
              • Sep 2006
              • 25

              #7
              the date is in the following format;

              date month year
              ie: 12 feb 2006

              Comment

              • pukur123
                New Member
                • Sep 2006
                • 61

                #8
                Actually all the filenames are hard coded. So make a file date.txt with the following data in the directory where the program is....

                24 may 2010
                12 aug 2007
                12 aug 2006
                15 aug 2006
                07 aug 2006
                12 aug 2005
                24 may 2003


                Before running the code below take the backup of the above file.


                #include<stdio. h>
                #include<string .h>

                void parse_date(char *, int *, char *, int *, int *);
                void sort_date(int, char *, int, int);

                int main()
                {
                FILE *fp;
                char ch, dstr[20], month[5];
                int i=0, dd, mm, yy, j;

                fp=fopen("date. txt", "r");

                while((ch=fgetc (fp)) != EOF)
                {
                if(ch != '\n')
                {
                dstr[i++]=ch;
                continue;
                }

                dstr[i]='\0';

                parse_date(dstr , &dd, month, &mm, &yy);

                sort_date(dd, month, mm, yy);

                i=0;
                }

                fclose(fp);

                remove("date.tx t");
                rename("date1.t xt", "date.txt") ;

                return 0;
                }




                void parse_date(char *dstr, int *dd, char *month, int *mm, int *yy)
                {
                char temp[5], mon[12][5]={"jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sept", "oct", "nov", "dec"};
                int i, j;

                j=0;
                i=0;

                while(dstr[i] != ' ') /* This loop will give the day */
                temp[j++]=dstr[i++];

                temp[j]='\0';

                *dd=atoi(temp);

                j=0;
                i++;
                while(dstr[i] != ' ') /* This loop will give the month */
                month[j++]=dstr[i++];

                month[j]='\0';

                for(j=0; j<12; j++)
                {
                if(strcasecmp(m onth, mon[j]) == 0)
                {
                *mm=j+1;
                break;
                }
                }

                j=0;
                i++;

                while(dstr[i] != '\0') /* This loop will give the year */
                temp[j++]=dstr[i++];

                temp[j]='\0';

                *yy=atoi(temp);
                }




                void sort_date(int dd, char *month, int mm, int yy)
                {
                static int flag1;
                FILE *fp, *ft;
                char ch, dstr[20], month1[5];
                int i=0, flag2, dd1, mm1, yy1;

                if(flag1 == 0)
                {
                fp=fopen("date1 .txt", "w");
                fprintf(fp, "%d %s %d\n", dd, month, yy);
                fclose(fp);
                flag1=1;
                }
                else
                {
                flag2=0;
                fp=fopen("date1 .txt", "r");
                ft=fopen("temp. txt", "w");

                while((ch=fgetc (fp)) != EOF)
                {
                if(ch != '\n')
                {
                dstr[i++]=ch;
                continue;
                }

                dstr[i]='\0';

                if(flag2 == 0)
                {
                parse_date(dstr , &dd1, month1, &mm1, &yy1);

                if(yy < yy1)
                flag2=1;
                else if(mm < mm1 && yy == yy1)
                flag2=1;
                else if(dd < dd1 && mm == mm1 && yy == yy)
                flag2=1;

                if(flag2 == 1)
                fprintf(ft, "%d %s %d\n", dd, month, yy);

                fprintf(ft, "%s\n", dstr);
                }
                else
                fprintf(ft, "%s\n", dstr);

                i=0;
                }

                if(flag2 == 0)
                fprintf(ft, "%d %s %d\n", dd, month, yy);

                fclose(fp);
                fclose(ft);

                remove("date1.t xt");
                rename("temp.tx t", "date1.txt" );
                }
                }

                Comment

                • kuldeep0110
                  New Member
                  • Aug 2022
                  • 1

                  #9
                  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.
                  (i) The dates are provided in the EU format DD-MMM-YYYY, like 20-Jun-2020.
                  (ii) The dates are provided in the US format MMM DD, YYYY, like aug 02, 2022.

                  can anybdy help me with the c program for above problem

                  Comment

                  • dev7060
                    Recognized Expert Contributor
                    • Mar 2017
                    • 656

                    #10
                    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.
                    (i) The dates are provided in the EU format DD-MMM-YYYY, like 20-Jun-2020.
                    (ii) The dates are provided in the US format MMM DD, YYYY, like aug 02, 2022.

                    can anybdy help me with the c program for above problem
                    Start a new thread and show what you have done so far.

                    Comment

                    Working...