This problem is twofold:
- How do you get sortable date values from string representations
- How can you effectively sort said values.
Finding valid timestamps from a date-string
C++ uses the time_t object as a valid number of seconds from a set date (Jan 1, 1970 UTC) There's plenty of concise information about that, in every-case you may consider this an Integer-representation of time in seconds.
Next you need to know how to parse your data into a time-stamp: here are some rather some helpful links.
My preferred method is mktime - there's an example of that Here on stack-overflow. Also it seems someone else has done the same course as you ;)
You might want to consider using A function of your own design, if the date format is unusual. In this case, using scanf is often the simplest way - the interface of this function is somewhat old-school "c-style", but that doesn't change the simple fact it works, and well.
Here's a link to someone reading a simple-date with scanf.
Turns out the code I wrote below is close to The answer to this great question
#include <stdio.h>
#include <time.h>
time_t GetDateFromObject(value & Date_Object)
{
char * Date_String = Date_Object.date.c_str();
int year, month, day, hour, minute, second;
if(sscanf(Date_String , "%d-%d-%d %d:%d:%d", &year, &month, &day, &hour, &minute, &second) == 6)
{
time_t rawTime;
time(&rawTime);
struct tm * parsedTime;
parsedTime = localtime(&rawTime);
// tm_year is years since 1900
parsedTime->tm_year = year - 1900;
// tm_months is months since january
parsedTime->tm_mon = month - 1;
parsedTime->tm_mday = day;
parsedTime->tm_hour = hour;
parsedTime->tm_min = minute;
parsedTime->tm_sec = second;
return mktime(parsedTime);
}
}
Association and sorting of dates
Once you know how to get a time_t from your date, you can start creating an associative array of the data - in this case I'll be using a map.
With that, here's an example of using a map to insert, sort, and output the data.
#include<iostream>
#include<map>
#include<vector>
#include<ctime>
struct value {
std::string code;
std::string date;
std::string name;
};
void Print_Range(std::vector<value> & Data, value & Date_Start, value & Date_end)
{
std::map<time_t, value *> Associated_Data;
for(auto Date_Object : Array_Of_Dates)
{
time_t Object_Time = GetDateFromObject(Date_Object);
Associated_Data.insert(std::make_pair(Object_Time, & Date_Object);
}
//as the std::map is sorted by-default,
//we can know locate the iterator for any two time codes
time_t Search_From = GetDateFromObject(Date_Start);
time_t Search_To = GetDateFromObject(Date_End);
auto Start_IT = Associated_Data.find(Search_From);
auto End_IT = Associated_Data.find(Search_To);
std::cout << "Printing all dates in range \n";
for(auto IT=Start_IT; IT != End_IT; IT++)
{
std::cout << IT->Second->date << '\n';
}
}
Notes:
- I use C++11 syntax here, if you don't understand Range based loops you might want to read up on them.
- I'm assuming the structure you described is stored in a vector.
- The "GetDateFromObject" function I'm using here is a placeholder for whatever function you use to get the timestamp)
- Inserting data into a map using std::make_pair.
- I'm holding pointers to the original value-objects.