What's the simplest way of defining lexicographic comparison for elements of a class?

Posted by the_mandrill on Stack Overflow See other posts from Stack Overflow or by the_mandrill
Published on 2010-03-23T14:28:10Z Indexed on 2010/03/23 14:43 UTC
Read the original article Hit count: 127

Filed under:
|

If I have a class that I want to be able to sort (ie support a less-than concept), and it has several data items such that I need to do lexicographic ordering then I need something like this:

struct MyData {
  string surname;
  string forename;

  bool operator<(const MyData& other) const {
    return surname < other.surname || (surname==other.surname && forename < other.forename); }
};

This becomes pretty unmanageable for anything with more than 2 data members. Are there any simpler ways of achieving it? The data members may be any Comparable class.

© Stack Overflow or respective owner

Related posts about c++

Related posts about lexicographic