I am looking for non-canonical sorts of lists. A function that would give:

Sort[X, "order type"]

With "order type" being colex, revlex, etc.

  • 1
    $\begingroup$ I assume your X's are list of numbers. Define a predicate that handles lex. Then many other orders can be attained by multiplying by an appropriate matrix. For example revlex would use a matrix with 1's on the antidiagonal and 0's elsewhere. $\endgroup$ – Daniel Lichtblau Aug 10 '13 at 23:24

I believe you can effect any of the lex, colex, revlex, revcolex sorts by reversing elements and lists in the proper sequence.

Edit: My original answer used Sort and Reverse. Since at the moment I cannot see why I did not use SortBy[x, Reverse] as Simon Woods did in an answer to a similar question I am modifying my answer to use the simpler method.

Reverse Lexicographic

The simplest variation, merely reverse the list after sorting:

Reverse @ Sort[x]


Reverse the elements before sorting, using SortBy:

SortBy[x, {Reverse}]

Reverse Colexicographic

Simply reverse the entire list in addition to the steps for the colex sort:

Reverse @ SortBy[x, {Reverse}]


A visualization function:

plot = ArrayPlot[SparseArray[List /@ # -> 1] & /@ #, ImageSize -> 80] &;

A random shuffle of subsets:

x = RandomSample @ Subsets[Range@7, {3}];

Our orderings:

lex      = Sort[x];
revlex   = Reverse @ Sort[x];
colex    = SortBy[x, {Reverse}];
revcolex = Reverse @ SortBy[x, {Reverse}];

A comparative graphic:

Row[plot /@ {lex, revcolex, colex, revlex}]

enter image description here

| improve this answer | |

A shorter version for reverse colexicographic sorting for numeric input:

SortBy[x, {-Reverse@# &}]

% == revcolex


| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.