Sort all expression levels

What is a good way to sort all levels of expression? The following does what I want when an expression has a rectangular structure, but I would like it to work for non-rectangular expressions as well

Map[Sort, {expr}, Depth[expr] - 1]

For example, the following should print True

sorted = deepSort[{{{1, 3, 8}, {3, 7, 6}, {10, 4, 9}, {3, 8, 10, 
      6}, {8, 2, 5, 10}, {8, 5, 10, 
      9}}, {{{1, 3, 8}, {3, 8, 10, 6}}, {{3, 7, 6}, {3, 8, 10, 
       6}}, {{10, 4, 9}, {8, 5, 10, 9}}, {{3, 8, 10, 6}, {8, 2, 5, 
       10}}, {{8, 2, 5, 10}, {8, 5, 10, 9}}}}];
checkSortedLevel[k_] := Map[OrderedQ, sorted, {k}];
And @@ Flatten[checkSortedLevel /@ Range[0, 2]]
+5
source share
2 answers

deepSort[expr_] := Map[Sort, expr, {0, -2}]

Note that this will work even if your expr contains chapters other than List.

+8
source

If you have an expression containing chapters other than List and you do not want to sort them, this can be useful.

expr /. List :> Composition[Sort, List]
+3
source

All Articles