# Are there functions or packages in Mathematica to sort subsets in lex order, colex, revlex, etc.?

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.

-
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. – 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]


### Colexicographical

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}]


## Examples

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}]


-