## Sort a List, in a List of Lists of Lists

### Sort a List, in a List of Lists of Lists

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

LP

----------------------------------------------------------------
This message was sent using IMP, the Internet Messaging Program.

### Sort a List, in a List of Lists of Lists

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

If the depth is variable: lists /. x_?VectorQ :> Sort[x]

If the depth is constant: Map[Sort, lists, {2}]

### Sort a List, in a List of Lists of Lists

Hi,

it's the function Map:

l = {{{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}}};
Map[Sort, l, {-2}]

Cheers
Patrick

### Sort a List, in a List of Lists of Lists

Am 13.11.2010 06:59, schrieb XXXX@XXXXX.COM :

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:= res1 = Map[Sort, lst, {2}]

In:= res2 = Map[Sort, lst, {-2}]

In:= res3 = (lst /. (l:{__Integer}) :> Sort[l])

In:= res1 == res2 == res3 == goal

Out= True

hth,

albert

### Sort a List, in a List of Lists of Lists

Hi,

if you take negative numbers for the level spec than it's (in the case
of -2) the second level "from the innerst level in outward direction"

l = {{{{4}}}};
Map[f, l, {2}]
Map[f, l, {-2}]

Cheers
Patrick

### Sort a List, in a List of Lists of Lists

Map[Sort, data, {2}]

Bob Hanlon

=============
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

LP

----------------------------------------------------------------
This message was sent using IMP, the Internet Messaging Program.

### Sort a List, in a List of Lists of Lists

Of course I tried this first, but neglected to notice the difference
between {2} and (2) (it is very hard to distinguish on my monitor

LP

Quoting Bob Hanlon < XXXX@XXXXX.COM >:

----------------------------------------------------------------
This message was sent using IMP, the Internet Messaging Program.

### Sort a List, in a List of Lists of Lists

Look up Map in the Documentation.

list = {{{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}}}

Map[Sort, list, {2}]

--

### Sort a List, in a List of Lists of Lists

A simple way to accomplish this is to use Map with a level
specification, that is

In:= Map[Sort, list, {2}]

Out= {{{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}}}

### Sort a List, in a List of Lists of Lists

A quick shot:

In:== a=={{{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}}};

In:== b=={{{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:== c==Sort/@Sort/@#&/@a

Out== {{{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:== b====c

Out== True

-Tomas

### Sort a List, in a List of Lists of Lists

Hi,

data = {{{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}}}

Mapping Sort on either level 2 or level -2 will do the job, automatically
doing the same
as your hand-written expression with many manually typed Sort commands:

In:= Map[Sort, data, {-2}]

Out= {{{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:= Map[Sort, data, {2}]

Out= {{{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}}}

Regards,
Leonid

### Sort a List, in a List of Lists of Lists

one way:

------------------
a={{{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}}};

{nRow,nCol,nPages}=Dimensions[a];
Map[Sort,Flatten[a,1]];
Partition[%, nCol]
------------------

Out= {{{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}}}

--Nasser

### Sort a List, in a List of Lists of Lists

ps. In the above, {nRow,nCol,nPages}=Dimensions[a] should really be {nPages,nRow,nCol}=Dimensions[a];

If you really want to do this in a more 'obvious' way, you can simply Sort each row in each page
by 'hand' as in

---------------
Table[ Sort[ a[[k,j]] ], {k,1,nPages},{j,1,nRow}]
--------------

Out= {{{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}}}

There is nothing wrong with this. It says go over each page in
the 3D structure, sort each row. The nice thing is that the
result will have the same shape as the original 3D matrix. So
no need to reshape it using Parition.

I think of a 3D matrix as made up of a book, the number of pages
is the size of the third dimension, and each page in the matrix has
size nRow,nCol. nRow tells how many lines on the page, nCol tells
how many columns in the page.

nPages actually is the first entry returned by Dimensions, not the third.

so, in 3D, when writing A[[ 1,2 ]] the '1' is the page
number, and '2' will be the row number on that page.

in 2D, A[[ 1,2 ]] then '1' is the row number, and '2' is the
column number. There is only one page, so no entry for it.

For 3D, A[[1,2,3]] means page 1, row 2 on that page, column 3 on that page.

For 4D matrix, A[[ n,p,i,j ]] is n'th book, p page in that book, XXXX@XXXXX.COM
i'th row on that page, j'th column

For 5D matrix A[[ m,n,p,i,j ]] is m library, n book in that library,
p page in that book, i row on that page, and j column.

etc...

If you think of the lists as the above, everything becomes very clear
and easy. At leat for me. I do not like to think of lists as nested and
levels and such. I always try to look at a list as arrays and matrices,
and look at what dimensions it has, and go from there.

--Nasser

### Sort a List, in a List of Lists of Lists

Hi,

you could use

data = {{{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}}};

Map[f, data, {2}]

{{f[{1, 2}], f[{2, 1}], f[{1, 1}]}, {f[{1, 1}], f[{1, 1}],
f[{1, 2}]}, {f[{2, 1}], f[{2, 2}], f[{1, 2}]}, {f[{2, 2}],
f[{1, 2}], f[{2, 2}]}, {f[{1, 1}], f[{2, 1}],
f[{1, 2}]}, {f[{1, 2}], f[{2, 2}], f[{2, 2}]}}

Map[Sort, data, {2}]

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

HTH,
Oliver

### Sort a List, in a List of Lists of Lists

Depending on the different structure of list you might face, this might
prove to be too ad-hoc, but you can have it while waiting for the good

listthing = {{{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}}};
Dimensions[%]

{6, 3, 2}

Map[Sort[#] &, listthing, Dimensions[listthing][]]

This should work for other types of lists:

Map[Sort[#] &, listthing, Min[Dimensions[listthing]] ]

It is by no means a general solution of course.

Best,

me

2010/11/13 < XXXX@XXXXX.COM >