Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8165873
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T19:40:22+00:00 2026-06-06T19:40:22+00:00

I have the following operator< that is supposed to sort first by a value,

  • 0

I have the following operator< that is supposed to sort first by a value, then by another value:

    inline bool operator < (const obj& a, const obj& b) 
    {
        if(a.field1< b.field1)
            return true;
        else
            return a.field2 < b.field2;
    }

I have the feeling this is incorrect and that you can’t do that without another third comparaison test on the members variables, but I can’t find any example where this doesn’t work.
So whould this really sort as expected?
thanks

edit :
I would have coded it as :

    inline bool operator < (const obj& a, const obj& b) 
    {
        if(a.field1< b.field1)
            return true;
                    else if(a.field1> b.field1)
            return false;
        else
            return a.field2 < b.field2;
    }

are there any differences? I’m asking because I know mine is correct from experience but also longer than the first one

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T19:40:24+00:00Added an answer on June 6, 2026 at 7:40 pm

    I’d like to do it all by myself..

    You should only compare the values of Obj::field2 if the values of Obj::field1 are equal.

    The easy-to-understand way:

    /* This will meet the requirements of Strict-Weak-Ordering */
    
    if (a.field1 != b.field1) return a.field1 < b.field1;
    else                      return a.field2 < b.field2;
    

    The correct (recommended) way:

    The "correct" way of implementing it uses only operator< to compare the fields, the below looks more complicated than it really is.

    It will however yield the same result as the easy-to-understand example previously written.

    return a.field1 < b.field1 || (
      !(b.field1 < a.field1) && a.field2 < b.field2
    );
    

    There must be a way of implementing operator< without causing a lot of headache?

    C++11 (and later)

    You can use std::tuple from the STL which already have operator< for multiple fields defined, such as in the below example.

    #include <utility>
    
    inline bool
    operator< (Obj const& lhs, Obj const& rhs)
    {
      return std::tie (lhs.field1, lhs.field2) < std::tie (rhs.field1, rhs.field2);
    }
    

    C++03

    If your compiler doesn’t have support for C++11 yet and you only need to compare two fields from each object you could use std::pair instead.

    The reason for std::make_pair is the same as in the previous example using std::tie.

    #include <utility>
    
    inline bool
    operator< (Obj const& lhs, Obj const& rhs)
    {
      return std::make_pair (lhs.field1, lhs.field2)
           < std::make_pair (rhs.field1, rhs.field2);
    }
    

    using std::pair will require copies of the members to be created, which in some circumstances is undesirable.

    Is this really recommended practise?

    See the below question/answers for more information, but to sum it up; the c++11 approach doesn’t cause that much overhead and it’s very simple to implement.

    • Implementing comparision operators via ‘tuple’ and ‘tie’, a good idea?
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following function/constructor/method (I'm not sure exactly what it is) List<T>& List<T>::operator=(const
I have the following code: template <typename T> LuaCall& operator>>(T) { BOOST_STATIC_ASSERT(sizeof(T) == 0);
I have the following code: template <class T> struct pointer { operator pointer<const T>()
I have the following functor: class ComparatorClass { public: bool operator () (SimulatedDiskFile *
In Excel, I have the following formula =(MIN(H69,H52,H35,H18)*(1/H18))*10 that is supposed to return the
I have the following OR operator, now currently if the c is null the
Is there a way to implement operator->, not only operator*. To have following code
why if we have pure virtual assignment operator in a base class, then we
Suppose that I have following class template<unsigned char B, unsigned char F> class Foo
I have the following code: /* * Pointer to a function that reads a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.