title: Defining < for STL sort algorithm - operator overload, functor or standalone function?
body: <p>I have a stl::list containing Widget class objects. They need to be sorted according to two members in the Widget class.</p>
  
  <p>For the sorting to work, a less-than comparator comparing two Widget objects must be defined. There seems to be a myriad of ways to do it. From what I can gather, one can either:</p>
  
  <p>a. Define a comparison operator overload in the class:</p>
  
  <pre><code>bool Widget::operator&lt; (const Widget &amp;rhs) const
  </code></pre>
  
  <p>b. Define a standalone function taking two Widgets:</p>
  
  <pre><code>bool operator&lt;(const Widget&amp; lhs, const Widget&amp; rhs);
  </code></pre>
  
  <p>And then make the Widget class a friend of it:</p>
  
  <pre><code>class Widget {
      // Various class definitions ...
      friend bool operator&lt;(const Widget&amp; lhs, const Widget&amp; rhs);
  };
  </code></pre>
  
  <p>c. Define a functor and then include it as a parameter when calling the sort function:</p>
  
  <pre><code>class Widget_Less :
  public binary_function&lt;Widget, Widget, bool&gt; { 
      bool operator()(const Widget &amp;lhs, const Widget&amp; rhs) const;
  };
  </code></pre>
  
  <p>Does anybody know which method is better? In particular I am interested to know if I should do 1 or 2. I searched the book Effective STL by Scott Meyer but unfortunately it does not have anything to say about this.</p>
  
  <p>Thank you for your reply.</p>