Re: Sort a List, in a List of Lists of Lists
- To: mathgroup at smc.vnet.net
 - Subject: [mg113803] Re: Sort a List, in a List of Lists of Lists
 - From: Albert Retey <awnl at gmx-topmail.de>
 - Date: Sun, 14 Nov 2010 06:06:35 -0500 (EST)
 - References: <ibl9g2$b8a$1@smc.vnet.net>
 
Am 13.11.2010 06:59, schrieb leigh.pascoe at inserm.fr:
> Dear Mathgroup,
> 
> I have a lsit of Lists of Lists:
> 
> {{{1,2},{2,1},{1,1}},{{1,1},{1,1},{1,2}},{{2,1},{2,2},{1,2}},{{2,2},{1,2},{2,2}},{{1,1},{2,1},{1,2}},{{1,2},{2,2},{2,2}}}
> 
> I would like to sort the elements in the lowest level of brackets to give
> 
> {{{1, 2}, {1, 2}, {1, 1}}, {{1, 1}, {1, 1}, {1, 2}}, {{1, 2}, {2,
>     2}, {1, 2}}, {{2, 2}, {1, 2}, {2, 2}}, {{1, 1}, {1, 2}, {1,
>     2}}, {{1, 2}, {2, 2}, {2, 2}}}
> 
> i.e retaining the same structure with the paired elements in the 
> original order. I can't seem to get the syntax right to do this apart 
> from the obvious
> 
> {{Sort[{1, 2}], Sort[{2, 1}], Sort[{1, 1}]}, {Sort[{1, 1}],
>    Sort[{1, 1}], Sort[{1, 2}]}, {Sort[{2, 1}], Sort[{2, 2}],
>    Sort[{1, 2}]}, {Sort[{2, 2}], Sort[{1, 2}],
>    Sort[{2, 2}]}, {Sort[{1, 1}], Sort[{2, 1}],
>    Sort[{1, 2}]}, {Sort[{1, 2}], Sort[{2, 2}], Sort[{2, 2}]}}
> 
> I must have a blind spot for the correct command. Can someone please 
> help me with what should be a straightforward sort. As I want to carry 
> this out on several hundred thousand pairs I need a more efficient 
> command. Thanks in advance.
here are three ways:
lst = {{{1, 2}, {2, 1}, {1, 1}}, {{1, 1}, {1, 1}, {1, 2}}, {{2,
    1}, {2, 2}, {1, 2}}, {{2, 2}, {1, 2}, {2, 2}}, {{1, 1}, {2,
    1}, {1, 2}}, {{1, 2}, {2, 2}, {2, 2}}}
goal = {{{1, 2}, {1, 2}, {1, 1}}, {{1, 1}, {1, 1}, {1, 2}}, {{1,
    2}, {2, 2}, {1, 2}}, {{2, 2}, {1, 2}, {2, 2}}, {{1, 1}, {1,
    2}, {1, 2}}, {{1, 2}, {2, 2}, {2, 2}}}
In[86]:= res1 = Map[Sort, lst, {2}]
In[88]:= res2 = Map[Sort, lst, {-2}]
In[89]:= res3 = (lst /. (l:{__Integer}) :> Sort[l])
In[90]:= res1 == res2 == res3 == goal
Out[90]= True
hth,
albert