Example matrix (rows always have equal lengths)
mat = {
{1, 2, 3, 4, 5},
{1, 1, 1, 1, 1},
{1, 3, 4, 7, 9},
{2, 2, 2, 2, 2},
{5, 4, 3, 2, 1},
{8, 4, 7, 9, 2},
{3, 2, 1, 2, 3},
{1, 3, 5, 7, 9}};
I tried to sort mat
in the following manner:
{{1, 1, 1, 1, 1}, (* All elements are equal *)
{2, 2, 2, 2, 2}, (* All elements are equal *)
{1, 2, 3, 4, 5}, (* Sorted sequence with equal differences of 1 *)
{5, 4, 3, 2, 1}, (* Reverse sorted sequence with equal differences of 1 *)
{1, 3, 5, 7, 9}, (* Sorted sequence with equal differences of 2 *)
{1, 3, 4, 7, 9}, (* Sorted sequence with unequal differences *)
{3, 2, 1, 2, 3}, (* Unsorted sequence with few sort swaps needed *)
{8, 4, 7, 9, 2}} (* Unsorted sequence with many sort swaps needed *)
The closest solution I found was:
SortBy[mat, Abs @* Differences]
giving
{{1, 1, 1, 1, 1},
{2, 2, 2, 2, 2},
{1, 2, 3, 4, 5},
{3, 2, 1, 2, 3},
{5, 4, 3, 2, 1},
{1, 3, 4, 7, 9},
{1, 3, 5, 7, 9},
{8, 4, 7, 9, 2}}
Looking at {3, 2, 1, 2, 3}
, which is in the wrong position, it becomes clear that
we need at least one additional sort parameter for unsorted sequences: Count the number of permutations needed to Sort
or ReverseSort
them, but I don't know how to implement this.
Probably my description of the problem is incomplete or ambiguous, so please feel free to suggest another "entropic sort order".
SortBy[mat, Entropy]
? i.e., let the built-in function determine the sort order. $\endgroup$