Fortunately, my comparator function is pretty cheap to run; I can run it as many times as I want. Ultimately, you can think of the output as a big set of tuples like (first, second, better) -- like (A, B, A) ("between A and B, A is better"). There would be N * (N - 1) such tuples.