title: Defining comparator for sort function in c++ for pair objects
body: <p>Below is my code. The operat function is not working properly. Any help will be appreciated. Normally, <code>sort</code> sorts in ascending order. I want to define operat such that it sorts in descending order</p>
  
  <pre><code>#include&lt;iostream&gt;
  #include&lt;cstdio&gt;
  #include&lt;algorithm&gt;
  #include&lt;vector&gt;
  #include&lt;utility&gt;
  using namespace std;
  typedef pair&lt;int,int&gt; pii;
  typedef pair&lt;int,pii&gt; pwp;
  
  bool operat(pwp a, pwp b){
      if(a.first &gt; b.first){
          return true;
      }else if(a.second.first &gt; b.second.first) {
          return true;
  
      }else if (a.second.second &gt; b.second.second) { return true;}
      return false;
  
  }
  int main(){
      vector&lt;pwp&gt; inp;
      pwp obj1 = make_pair(1998,make_pair(3,24));
      pwp obj2 = make_pair(1998,make_pair(3,21));
      pwp obj3 = make_pair(1997,make_pair(3,24));
      inp.push_back(obj1);
      inp.push_back(obj2);
      inp.push_back(obj3);
      printf("Before sorting\n");
      for(int i = 0 ; i&lt; inp.size();i++){
          pwp sth = inp[i];
          printf("%d %d %d\n",sth.first,sth.second.first,sth.second.second);
  
      }
      sort(inp.begin(), inp.end(),operat);
      cout&lt;&lt;"After soring"&lt;&lt;endl;
      for(int i = 0 ; i&lt; inp.size();i++){
          pwp sth = inp[i];
          printf("%d %d %d\n",sth.first,sth.second.first,sth.second.second);
      }
  return 0;
  }
  </code></pre>
  
  <p>New one:</p>
  
  <pre><code>bool operat(pwp a, pwp b){
      if(a.first &gt; b.first){
          return true;
      }else if(a.first &lt;  b.first) return false;
  
      else if(a.second.first &gt; b.second.first) {
          return true;
  
      }else if (a.second.first &lt; b.second.first) return false;
      else if (a.second.second &gt; b.second.second) { return true;}
      return false;
  
  }
  </code></pre>