473,408 Members | 2,477 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

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?
Sep 19 '06 #1
9 21052
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.
Sep 19 '06 #2
how can i do this..

i am just a beginner in c.

not sure how should i do it
Sep 19 '06 #3
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.
Sep 19 '06 #4
can u give me an example;

say that the dates are:
12 aug 2006
13 aug 2006
11 aug 2006
09 aug 2006
Sep 19 '06 #5
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.
Sep 19 '06 #6
the date is in the following format;

date month year
ie: 12 feb 2006
Sep 19 '06 #7
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.txt");
rename("date1.txt", "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(month, 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.txt");
rename("temp.txt", "date1.txt");
}
}
Sep 19 '06 #8
kuldeep0110
1 Bit
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
Aug 6 '22 #9
dev7060
636 Expert 512MB
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.
Aug 19 '22 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: bissatch | last post by:
Hi, I am about to write an application that will display all pdf files in a folder and display them by file name in order of date. Is there an easy way of doing this? I was thinking about...
1
by: John Taylor | last post by:
I have a ListCtrl with 5 columns. The first 4 columns are either strings or integers, but the last column is a string in the format of MM-DD-YYYY. I searched google and also read over the...
9
by: p0wer | last post by:
Let's suppose I have this sample document: <root> <entry id="1" <date>2003-08-03</date> <param_1>5</param_1> <param_2>10</param_2> </entry> <entry id="2"> ...
17
by: Matt Kruse | last post by:
I'm looking for the best JS/CSS solution to add functionality to tables. The only browser which needs to be supported is IE5.5+, but no activeX can be used. to be able to do: - Fixed header row...
22
by: mike | last post by:
If I had a date in the format "01-Jan-05" it does not sort properly with my sort routine: function compareDate(a,b) { var date_a = new Date(a); var date_b = new Date(b); if (date_a < date_b)...
18
by: Scott | last post by:
I have a collection where the items in the collection are dates. I want to iterate over the collection and build a value list string for the rowsource of a listbox. The dates in the collection are...
3
by: melanieab | last post by:
Hi, When you click on a column header to sort in ascending or descending order, numbers (and dates) aren't sorted correctly. It turns up like this: 1 10 11 2 3 4
19
by: Owen T. Soroke | last post by:
Using VB.NET I have a ListView with several columns. Two columns contain integer values, while the remaining contain string values. I am confused as to how I would provide functionality to...
5
by: Mike | last post by:
I have several datagirds that allow sorting on two date columns. The sorting works but its not showing the newest date first when I sort. For example if I have dates in my column as 01/01/2008...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.