This is implemented in SortBy
:
Because this function does not perform a pairwise compare, you would need to be able to recast your sort function to produce a canonical ordering. On the upside, if you are able to do so it will be far more efficient than Sort
.
f1 = Mod[#, 4] &;
f2 = Mod[#, 7] &;
SortBy[Range@10, {f1, f2}]
{#, f1@#, f2@#} & /@ % // Grid
{8, 4, 1, 9, 5, 2, 10, 6, 7, 3}
$\begin{array}{r} 8 & 0 & 1 \\ 4 & 0 & 4 \\ 1 & 1 & 1 \\ 9 & 1 & 2 \\ 5 & 1 & 5 \\ 2 & 2 & 2 \\ 10 & 2 & 3 \\ 6 & 2 & 6 \\ 7 & 3 & 0 \\ 3 & 3 & 3 \end{array}$
Also see: