title: Why do we include compare function inside definition of struct while sorting vector of custom objects?
body: <p>Why do we do this:</p>
  
  <pre><code>struct MyStruct
  {
      int key;
      std::string stringValue;
  
      MyStruct(int k, const std::string&amp; s) : key(k), stringValue(s) {}
  };
  
  struct less_than_key
  {
      inline bool operator() (const MyStruct&amp; struct1, const MyStruct&amp; struct2)
      {
          return (struct1.key &lt; struct2.key);
      }
  };
  
  std::vector &lt; MyStruct &gt; vec;
  
  vec.push_back(MyStruct(4, "test"));
  vec.push_back(MyStruct(3, "a"));
  vec.push_back(MyStruct(2, "is"));
  vec.push_back(MyStruct(1, "this"));
  
  std::sort(vec.begin(), vec.end(), less_than_key());
  </code></pre>
  
  <p>Here in the above code snippet why is the <code>operator()</code> function included in <code>struct less_than_key</code>. What happens if I take it out of the struct definition?  </p>