Ensure there is a complete ordering on index elements.

As qsort isn't stable, unless the index elements have a complete
ordering the sorting is non-deterministic, which leads to all
manner of problems.