Custom sort based on 3 objects : <p>I would like to sort a 2D array based on the similarity between elements compared to the element at index 0.</p>

<p>How is a custom sort function defined that can use the external 3rd object, the element at index 0?</p>

<p>The compare function needs to do the following</p>

<pre><code>bool compare(float *a, float *b) {
    float *source = 2DArray[0];
    return distance(source, a) &gt; distance(source, b);
}
</code></pre>

<p>when calling it </p>

<pre><code>std::sort(std::begin(2DArray), std::end(2DArray), compare);
</code></pre>

<p>there is obviously no access to 2DArray.</p>

<p>How can this be written so that an object can call a function to call the sort function and give access to the 2DArray[0]?</p>
