define comparison operator at run time

2010-11-19
2013-04-25
  • I do not know in advance on which fields my dataset should be sorted, the sort fields are defined only by the user of the program. Is it possible to somehow define a comparison operator at run time ?

     
  • Well, you can define a functor which decides at run-time what to compare, maybe something like this:

    bool operator()(const Structure& a, const Structure& b) const
    {
       Key key;

       switch(key)
       {
         case lastname:
             return a.lastname < b.lastname;
         case firstname:
             return a.firstname < b.firstname;
          …
        }
    }

    Remember that max_value() and min_value() have to work correspondingly, though maybe a switch is not necessary.

    Johannes