UPDATED: I have followed John's guidance and modified his code which solved my problem via creating a comparator function and and insert it to the Compare parameter in STL map. Since my string date are strictly in the shown format, using substr will be fine. My output and codes are below for your reference.
Date Total Sales
01JAN1900 $4
20JAN1902 $40
18NOV1912 $2500
19NOV1912 $2500
19OCT1923 $25
01JAN1991 $22
15NOV1991 $300
Grand Total: $5391
struct CompareDates
:
public std::binary_function <bool, std::string, std::string>
{
bool operator() (const std::string& lhs, const std::string& rhs)
{
if(lhs.substr(5,4) < rhs.substr(5,4))
{
return true;
}
else if (lhs.substr(5,4) == rhs.substr(5,4) && lhs.substr(2,3) < rhs.substr(2,3))
{
return true;
}
else if (lhs.substr(5,4) == rhs.substr(5,4) && lhs.substr(2,3) == rhs.substr(2,3) && lhs.substr(0,2) < rhs.substr(0,2))
{
return true;
}
else
{
return false;
}
}
};
map<string, double,CompareDates> dailyDatePrices;
Initial Problem: I am required to sort raw data into a daily report format. As such I have used STL map
to store the date as the key and the item price as the value. From what I read, STL map is automatically sorted. However I do not want it to be sorted by map as it will generate the undesired current report output stated below. I will want to sort based on the string date(earliest to latest) and will want it to be that exact format. I have used vector and function comparator to sort the date already before using map. Is there any way to go about doing it? Thanks!
Raw Data
STRAW:10:15NOV1991
TOY:10:15NOV1991
BARLEY:5:01OCT1992
Undesired Current Report Output
01OCT1992 5
15NOV1991 20
Expected Report Output
15NOV1991 20
01OCT1992 5