Amit Kumar
Amit Kumar

Reputation: 1507

Custom comparison function for SORT in C++ STL?

I am using

       typedef pair < long , pair <bool ,long > > t;
       vector <t> time ;

and I need to sort the above vector using std::sort() function but using a a customized comparison function. I wrote a version of it but is not working properly. Can you please point out my mistake?

     bool comp ( const t &a , const t &b){

         if ( a.first < b.first)
             return a.first > b.first ;
         else if ( b.first < a.first )
             return b.first > a.first ;
         else if ( a.first == b.first )
         {
             if ( a.second.first == false && b.second.first == true)
                return a.first > b.first ;
             else if ( a.second.first == true && b.second.first == false)
                return b.first > a.first ;
             else
                return a.first > b.first ;
          }
      }


      inside main(){
         sort ( time.begin() ,time.end() ,comp) ;
      }

Custom Case:

Before sorting : vector time

       10 1 1 
      100 0 1
      100 1 2
      200 0 2
      150 1 2
      500 0 2
      200 1 2
      300 0 2

After Sorting:

      10 1 1
      100 0 1
      100 1 2
      150 1 2
      200 0 2
      200 1 2
      300 0 2
      500 0 2

Upvotes: 0

Views: 882

Answers (2)

James Kanze
James Kanze

Reputation: 153919

Your comparison function doesn't define an ordering. In fact, it seems to return true any time a.first != b.first.

I'm not sure what sort of custom order you want. The standard ordering of std::pair will result in something like:

bool
comp( t const& a, t const& b )
{
    if ( a.first != b.first )
        return a.first < b.first;
    else if ( a.second.first != b.second.first )
        return a.second.first < b.second.first;
    else 
        return a.second.second < b.second.second;
}

It's actually a bit more complicated, since the only comparison operator it uses is <. But if < and != are both available, and behave normally, the results are the same as the above.

You can easily adopt this to compare the elements in whatever order you want; if you want to inverse the order for one of the elements, just replace < with >.

And finally, false compares less than true for boolean values.

Upvotes: 1

Kit Fisto
Kit Fisto

Reputation: 4515

It should be:

if ( a.first < b.first)
    return true
else if ( b.first < a.first )
    return false;
// etc.

In your version it returns false in both cases.

Upvotes: 3

Related Questions