I want to order a bunch of number sequences lexicographically. Did this with
lexo[x_, y_] := x == {} ||
y != {} && (First[x] < First[y] || First[x] == First[y] && lexo[Rest[x], Rest[y]])
Sort[mysequences, lexo]
and it works, but it is presumably not very efficient, because of recursive self-calls.
There is also a version in one of the answers here which might be better. But maybe Mathematica has something built in for that?
I suspect this since there turns out to be something called Lexicographic for which I could not find any documentation. Also, Combinatorica has things like LexicographicSubsets and LexicographicPermutations except that I want something more general, working for any sequences of numbers.
SortBy[First]@mysequences? – kglr Mar 17 '21 at 09:51Lexicographicis an option value for theMonomialOrderoption inGroebnerBasis. Nowadays it would be a string but at the time this was designed we really didn't do option settings that way. (2) Coincidently, implementing aLexicographicOrderfor usage inSortand related is on my immediate to-do list. – Daniel Lichtblau Mar 17 '21 at 13:17Subsets[Range[3]], throws an error message because of the empty set, and sorts like this{{1},{1,2},{1,3},{1,2,3},{2},{2,3},{3},{}}which is not quite lexicographic:{1,2,3}must go before{1,3}– მამუკა ჯიბლაძე Mar 17 '21 at 23:10SortBy[x \[Function] PadRight[x, Max[Length /@ #]]]@# &@ Subsets[Range@3]? – kglr Mar 17 '21 at 23:22(x \[Function] SortBy[x, PadRight[#, Max[Length /@ x]] &])@ Subsets[Range@3]? – kglr Mar 17 '21 at 23:26