title: How to add a comparator to a custom sort function
body: <p>So I've implemented merge sort which, for all intends and purposes, could also be a custom sort function and I've started turning it into a template function. </p>
  
  <p>Where I've run into a problem is when I wanted to add to possibility of passing a custom compare function in order to sort in different ways. (eg. std::greater and std::less or any custom one). </p>
  
  <p>I've verified that the sorting algorithm works when I'd replace the ints by T. How would I add the custom compare function from here in order to also sort custom objects etc?</p>
  
  <pre><code>template &lt;  typename T, 
              class Compare&gt;
  void merge( vector&lt;T&gt; &amp;arr, int start, int mid, int end, Compare comp ) 
  {
      int lptr = start; 
      int rptr = mid+1; 
      int tempptr = 0; 
  
      vector&lt;T&gt; temp( end - start + 1 ); 
  
      for ( int i = 0; i&lt;temp.size(); i++)
      {
          if ( lptr &gt; mid ) //done with left-section, just move the right elements
          {   
              temp[tempptr] = arr[rptr];
              rptr++;
          } else if ( rptr &gt; end ) //done with right-section, just move the left elements
          {
              temp[tempptr] = arr[lptr];
              lptr++; 
          } else if ( comp( arr[rptr], arr[lptr] )) // right item &lt; left item, move right item
          {
              temp[tempptr] = arr[rptr]; 
              rptr++; 
          } else          //otherwise left item &lt; right item, move left item
          {
              temp[tempptr] = arr[lptr];
              lptr++; 
          }
          tempptr++;
      }
  
      for ( int i = 0; i&lt;temp.size(); i++)
      {
          arr[start + i] = temp[i]; 
      }
  }
  
  
  
  
  
  
  template &lt;  typename T, 
              class Compare&gt;
  void mergeSort( vector&lt;T&gt; &amp;arr, int start, int end, Compare comp)
  {   
  
      //if we're down to single elements, do nothing
      if ( start &lt; end ){
          //call to right and left 'child' 
          int mid = (start + end) / 2; 
  
          mergeSort( arr, start, mid ); 
          mergeSort( arr, mid + 1, end );
  
          //call to merge
          merge( arr, start, mid, end ); 
      }
  }
  
  
  
  int main()
  {   
      vector&lt;float&gt; arr = {7,8, 2, 6.6, 1, 4.1, 5, 3, 8, 9};
      cout &lt;&lt; "before sorting:" &lt;&lt; endl;
      for ( auto n : arr ) 
          cout &lt;&lt; n &lt;&lt; ", ";
      cout &lt;&lt; endl;
      mergeSort( arr, 0, arr.size() - 1); 
  
      cout &lt;&lt; "after sorting:" &lt;&lt; endl;
      for ( auto n : arr ) 
          cout &lt;&lt; n &lt;&lt; ", ";
      cout &lt;&lt; endl;
  
      return 0; 
  };
  </code></pre>
  
  <p>Thanks in advance.</p>