sorting a date array

I'm currently attempting to sort a two dimensional array of dates.

char dateAdded[20][9]

the data looks like 04/06/2008

this means i need to tear this char array apart into three different 2-d arrays, one for the month, day and year. then sort it first by year, then by month, then by day.

How to go about this is what stumps me. dateAdded is part of a struct. so i use a for loop to extract the data that i need. For example:

//book is the global struct

char year[20][4];

for(int i=0;i<20;i++)
{
year[i][0] = book[i].dateAdded[6];
year[i][1] = book[i].dateAdded[7];
etc.
}
my problem is that when i go to output one record of year it prints all twenty values.

//example output code:
cout<<year[0]<<endl;

//output:
20082008199919752006 etc.

all the dates are slammed together. When i output the second record it looks like:

year[1] = 2008199919752006 etc.

It skips the first "2008".

Help would be appreciated as to either what my error is, or a different more efficient method. Thank you
I think you meant
char year[20][4][11];

Why don't you convert the dates to ISO format (YYYY-MM-DD) for sorting?
Well i don't understand or have never heard of that. Do you have a link for an example. I will look for one as well. Thank you though!
Um... There's nothing to explain. Just reorder the characters in each string so that a date looks like this: 2009-04-27. the ISO date format has the advantage that it can be sorted using a traditional string comparison algorithm.
It can be done in a few steps by moving the data around char arrays.
Last edited on
Thank you. I will try this. Is there any example you have?
my problem now does not lay in sorting the array. I understand the strcmp. My problem is placing the dates into a new array to be sorted (rearranging them so that year is first).
Take note below this is while using the debugger that index 0 is edited while it should only be editing index 1. Also, char is a length of 10 and has a max of 20 records. I didn't show all 20 records because they are not the problem, the char array is bigger than 10 for some reason???
+ [0] 0x0026f51c "2008-04-222ÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌ" char [10]
+ [1] 0x0026f526 "2ÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌ" char [10]
You overwrote the 0 at the of the array.
I'm feeling generous, so I'll tell you how.
From src[11] (MM/DD/YYYY) to dst[11] (YYYY-MM-DD):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <cstring>

int main(){
	char src[]="04/24/2009";
	char dst[11];
	//fill the array with '-'
	memset(dst,'-',10);
	//set the terminating character
	dst[10]=0;
	//copy YYYY
	memcpy(dst,src+6,4);
	//copy MM
	memcpy(dst+5,src,2);
	//copy DD
	memcpy(dst+8,src+3,2);
	std::cout <<src<<std::endl<<dst<<std::endl;
	return 0;
}
Last edited on
yes i understand what your saying. I haven't seen this memcpy() function as this is the first i've used c++. Thank you very much for you help.

If your interested in what i did, i made a char sdate[20][11] to hold 20 dates of length 10.
from there i input each character from the book struct into the sdate array. In position 11 ([10]) i placed the null character ('\0'). This fixed the problem.

Now onto sorting. Thank you!
Topic archived. No new replies allowed.