title: Sort stl vector containing template user defined class in decending order using function object
body: <p>I am facing an issue while sorting a vector of user defined object. I am using the sort algo defined by stl and passing it my function object but it refuses to compile, would you please help me with this.</p>
  
  <p>Thanks.</p>
  
  <pre><code>#include&lt;iostream&gt;
  #include&lt;vector&gt;
  #include&lt;algorithm&gt;
  
  using namespace std;
  
  template&lt;typename T&gt;
  class MyType
  {
      public:
      T t1;
      public:
      MyType()
      {
  
      }
      MyType(const MyType&amp; mt)
      {
          t1 = mt.t1;
      }
      MyType&amp; operator=(const MyType&amp; mt)
      {
          this-&gt;t1 = mt.t1;
          return *this;
      }
      bool operator&lt;(const MyType&amp; mt)
      {
          if(this-&gt;t1 &lt; mt.t1)
              return true;
          else
              return false;
      }
      bool operator==(const MyType&amp; mt)
      {
          if(this-&gt;t1 == mt.t1)
              return true;
          else
              return false;
      }
      MyType(T t)
      {
          t1 = t;
      }
  };
  
  template&lt;class T&gt;
  class cmp_greater
  {
      public:
      bool operator()(const T&amp; a, const T&amp; b)
      {
          return !(a &lt; b);
      }
  };
  
  int main()
  {
      MyType&lt;int&gt; m1(1);
      MyType&lt;int&gt; m2(2);
      MyType&lt;int&gt; m3(3);
  
      vector&lt;MyType&lt;int&gt; &gt; vmt;
  
      vmt.push_back(m1);
      vmt.push_back(m2);
      vmt.push_back(m3);
  
      vector&lt;MyType&lt;int&gt; &gt;::iterator pos;
  
      for(pos = vmt.begin(); pos != vmt.end(); pos++)
      {
          cout&lt;&lt;pos-&gt;t1&lt;&lt;endl;
      }
  
      sort(vmt.begin(), vmt.end(), cmp_greater&lt;MyType&lt;int&gt; &gt;() );
      cout&lt;&lt;"After sorting in decending order."&lt;&lt;endl;
      for(pos = vmt.begin(); pos != vmt.end(); pos++)
      {
          cout&lt;&lt;pos-&gt;t1&lt;&lt;endl;
      }
  
      vmt.erase(vmt.begin()+1);
  
      cout&lt;&lt;"after erase"&lt;&lt;endl;
  
      for(pos = vmt.begin(); pos != vmt.end(); pos++)
      {
          cout&lt;&lt;pos-&gt;t1&lt;&lt;endl;
      }
  
      //insert can also be used in vectors;
  }
  </code></pre>