title: C++ std list sort with custom comparator that depends on an member variable for the object instance
body: <p>Class:</p>
  
  <pre><code>Class:
    private:
      ...
      vector&lt;string&gt; words; 
      vector&lt; list&lt;int&gt; &gt; vints;
    public:
      myFunction(...)
  </code></pre>
  
  <p>I am calling a sort on non-empty list in another member function:</p>
  
  <pre><code>void myClass::myFunction (...) {
      ...
      if (!vints[i].empty()) vints[i].sort(sortFunc);
      ...
  }
  </code></pre>
  
  <p>My sorting function:</p>
  
  <pre><code>bool myClass::sortFunc(const int&amp; i, const int&amp; j) { return (words[i] &lt; words[j]); }
  </code></pre>
  
  <p>The Error:</p>
  
  <pre><code>error: no matching function for call to ‘std::list&lt;int, std::allocator&lt;int&gt;      &gt;::sort(&lt;unresolved overloaded function type&gt;)’
  /usr/include/c++/4.4/bits/list.tcc:301: note: candidates are: void std::list&lt;_Tp,     _Alloc&gt;::sort() [with _Tp = int, _Alloc = std::allocator&lt;int&gt;]
  /usr/include/c++/4.4/bits/list.tcc:378: note:                 void std::list&lt;_Tp, _    Alloc&gt;::sort(_StrictWeakOrdering) [with _StrictWeakOrdering = bool (SuperWordSearch::*)    (const int&amp;, const int&amp;), _Tp = int, _Alloc = std::allocator&lt;int&gt;]
  </code></pre>
  
  <p>I have researched and come across the following questions: </p>
  
  <p><a href="https://stackoverflow.com/questions/1415608/c-custom-compare-function-for-listsort">C++ Custom compare function for list::sort</a></p>
  
  <p><a href="https://stackoverflow.com/questions/4359587/problem-sorting-a-list-of-pointers">Problem sorting a list of pointers</a></p>
  
  <p><a href="https://stackoverflow.com/questions/1100671/error-in-stdlistsort-with-custom-comparator-expected-primary-expression-bef">Error in std::list::sort with custom comparator (expected primary-expression before &#39;)&#39; token)</a></p>
  
  <p>and they would have been sufficient had it not been for the fact that in this class, the sortFunc depends on the member variable WORDS for that instance of the object. So I cannot make the comparator function (sortFunc) static or global</p>
  
  <p>EDIT: Just came across this <a href="https://stackoverflow.com/questions/8282166/how-to-sort-a-stdlist-when-you-need-member-data">How to sort a std:list when you need member data?</a> and it provides a solution by making a friend class, but is it possible to accomplish this inside the user-defined class itself?</p>