# Are there functions or packages in Mathematica to generate 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 = Lex, 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 at 23:24

Your question title says "generate subsets" but your question body asks for and shows pseudocode for sorting. These seem to me related but different problems.

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

### Reverse Lexicographic

The simplest variation, merely reverse the list after sorting:

Reverse @ Sort[x]


### Colexicographical

Reverse the elements before sorting, then again afterward to restore them:

Reverse /@ Sort[Reverse /@ x]


This will be more efficient without Map, written:

Reverse[Sort[Reverse[x, 2]], 2]


Or as I prefer with infix notation:

Sort[x ~Reverse~ 2] ~Reverse~ 2


### Reverse Colexicographic

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

Sort[x ~Reverse~ 2] ~Reverse~ {1, 2}


## 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    = Sort[x ~Reverse~ 2] ~Reverse~ 2;
revcolex = Sort[x ~Reverse~ 2] ~Reverse~ {1, 2};


A comparative graphic:

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


-