I have a list which contains strings. The strings contain dates, and are of the form "12 March 2011". I want to find a way in which I can sort these strings into date order, but I am pretty stuck.
I know that I need to be using std::sort, but I don't know how to deal with the form of the string. I had the idea that I could convert each string into yyyymmdd in integer form and then do a simple sort, but I was wondering if there is an easier way than that.
That might be the easiest way; however, you could convert the strings into time_t or even struct tm, then you would have everything in date fields. Look at time.h, and man -a time.
Whatever you do, it would make sense to write a function that parses the date string to an int or a time_t. I would prob. not use a struct tm, as that will make the comparison logic more involved.
Once you save you function you could then use it to pre-parse all the dates to time_t values and then sort the new array, as you suggested. But then you'll have to format the results to display them.
The alternative is to use the conversion function to create a Compare function or functor to use with std::sort. This will be slower as each comparison during the sort will re-trigger the comparison function.
The latter might be a suitable for small number of elements. Once you have your parsing function, it should be easy enough to code both approaches.