Date sorting algorithm help

EDIT: Still stuck, please help!

I've been attempting to write a program to keep track of my workouts and do a number of different functions. The program uses the STL list to store a struct of data on my workout. Im having trouble thinking of an algorithm to sort my list and display it according to the date. The struct holds the date as a string the format (00/00/00). Could anyone help get me started on how i would go about sorting the list?

if a little visual help is needed, think of it like this, i have 4 objects in my linked list, these have the dates:
04/15/12
04/07/12
05/02/12
04/26/12

How would i sort these in a function and display them?


Last edited on
Maybe parse the string on input and store the month, day and year as ints.
Edit: If you want to keep them as strings I suppose you could just compare the individual char's.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <string>
#include <algorithm>
#include <list>

std::string mm_dd_yy_to_key( std::string date ) // invariant: date is dd/mm/yy, yy is 2000+yy
{
  std::rotate( date.begin(), date.begin()+6, date.end() ) ;
  return date ;
}

int main()
{
    std::list<std::string> dates = { "04/15/12", "04/07/12", "05/02/12", "04/26/12" } ;
    dates.sort( []( const std::string& a, const std::string& b )
                  { return mm_dd_yy_to_key(a) < mm_dd_yy_to_key(b) ; } ) ;
}
Thanks for the reply guys!

@JLBorges,

could you please explain this line?
1
2
 dates.sort( []( const std::string& a, const std::string& b )
                  { return mm_dd_yy_to_key(a) < mm_dd_yy_to_key(b) ; } ) ;


Last edited on
stuck!
stuck!
That's not very descriptive, what are you trying? Showing your code may help.
> could you please explain this line?

Uses std::list::sort() (predicate version).
http://en.cppreference.com/w/cpp/container/list/sort


1
2
[]( const std::string& a, const std::string& b )
                  { return mm_dd_yy_to_key(a) < mm_dd_yy_to_key(b) ; }

is a lambda expression
http://msdn.microsoft.com/en-us/library/dd293608.aspx

The code is functionally equivalent to:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <string>
#include <algorithm>
#include <list>

struct cmp_date_string
{
    static std::string mm_dd_yy_to_key( std::string date ) 
    {
      std::rotate( date.begin(), date.begin()+6, date.end() ) ;
      return date ;
    }

    inline bool operator() ( const std::string& a, const std::string& b ) const
    { return mm_dd_yy_to_key(a) < mm_dd_yy_to_key(b) ; }
};


int main()
{
    std::list<std::string> dates = { "04/15/12", "04/07/12", "05/02/12", "04/26/12" } ;
    dates.sort( cmp_date_string() ) ;
}
Thanks a ton for that explanation! it really helped.
So i got the code working, which i really appreciate it. But how would i sort this if the string was a part of a struct in a list of structs? Im guessing i might need to make a temp list to sort those dates, then somehow sort the main list using the second list? Thanks if you can help further guys!
I would overload operator< in the struct or write a comparetor function and only do comparison on the string member. And use std::sort to do the soring, if you write a comparator you have to pass it to sort()

Edit: Simplified example
1
2
3
4
5
6
7
8
9
10
 struct foo{
    int a,b,c;
    double d;
    std::string s;
    bool operator<(foo& f){
        if(s<f.s)
            return true;
        return false;
    }
}; 


Obviously you will want a different comparison in the operator.
Last edited on
Topic archived. No new replies allowed.