For Programmers: Free Programming Magazines  


Home > Archive > Mathematica > June 2005 > Sorting on multiple columns within a matrix









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Sorting on multiple columns within a matrix
Lee Newman

2005-06-09, 9:01 am

Hello,

I can't come up with a solution to the following simple task:

- I have a matrix, let's M={ {"Z",2,"a"},{"A",2,"k"}, {"Z",1,"z"},
{"A",1,"a"},{"Z",3,"k"}, {"A",3,"z"} }
- I want to be able to sort the matrix row-wise, based on multiple
columns, for example sort first on column 2 (ascending) and then
secondly on column1 (ascending)

desired result:
A 1 a
Z 1 z
A 2 k
Z 2 a
A 3 z
Z 3 k

I suspect the solution involves using a pure function of some form as
the second argument in the Sort function, but I can't figure out what
this function should be.

Thanks,
Lee

Jens-Peer Kuska

2005-06-09, 9:01 am

Hi,

Sort[M, If[#1[[2]] =!= #2[[2]], #1[[2]] < #2[[2]],
#1[[1]] < #2[[1]]] &]

??

Regards

Jens

"Lee Newman" <leenewm@umich.edu> schrieb im
Newsbeitrag news:d892lm$sc6$1@smc.vnet.net...
> Hello,
>
> I can't come up with a solution to the following
> simple task:
>
> - I have a matrix, let's M={
> {"Z",2,"a"},{"A",2,"k"}, {"Z",1,"z"},
> {"A",1,"a"},{"Z",3,"k"}, {"A",3,"z"} }
> - I want to be able to sort the matrix
> row-wise, based on multiple
> columns, for example sort first on column 2
> (ascending) and then
> secondly on column1 (ascending)
>
> desired result:
> A 1 a
> Z 1 z
> A 2 k
> Z 2 a
> A 3 z
> Z 3 k
>
> I suspect the solution involves using a pure
> function of some form as
> the second argument in the Sort function, but I
> can't figure out what
> this function should be.
>
> Thanks,
> Lee
>



dh

2005-06-09, 9:01 am

Hello Lee,
You need "Ordered" like in this example:


Sort[m, If[#1[[2]] === #2[[2]], OrderedQ[{#1[[1]], #2[[1]]}],
OrderedQ[{#1[[2]], #2[[2]]}]] &]

sincerely, Daniel

Lee Newman wrote:
> Hello,
>
> I can't come up with a solution to the following simple task:
>
> - I have a matrix, let's M={ {"Z",2,"a"},{"A",2,"k"}, {"Z",1,"z"},
> {"A",1,"a"},{"Z",3,"k"}, {"A",3,"z"} }
> - I want to be able to sort the matrix row-wise, based on multiple
> columns, for example sort first on column 2 (ascending) and then
> secondly on column1 (ascending)
>
> desired result:
> A 1 a
> Z 1 z
> A 2 k
> Z 2 a
> A 3 z
> Z 3 k
>
> I suspect the solution involves using a pure function of some form as
> the second argument in the Sort function, but I can't figure out what
> this function should be.
>
> Thanks,
> Lee
>


Lee Newman

2005-06-10, 9:00 am

dh -- thanks, your solution works, but it seems it might be awkward to
extend it to many more than two columns.

I am wondering if anyone knows a way to generalize dh's solution such
that it could be applied as a pure function to a list which contains the
columns to sort? I noodled over it but was unable to figure it out.
Something like:

Sort[matrix, puresortfunc[#] ]& /@ {1,14,2,4}

where 1,14,2,4 are the columns to use for sorting, and puresortfunc is
this tricky sorting function that would successively map # to the
elements in the list of columns to sort.


Lee


Lee Newman wrote:
> Hello,
>
> I can't come up with a solution to the following simple task:
>
> - I have a matrix, let's M={ {"Z",2,"a"},{"A",2,"k"}, {"Z",1,"z"},
> {"A",1,"a"},{"Z",3,"k"}, {"A",3,"z"} }
> - I want to be able to sort the matrix row-wise, based on multiple
> columns, for example sort first on column 2 (ascending) and then
> secondly on column1 (ascending)
>
> desired result:
> A 1 a
> Z 1 z
> A 2 k
> Z 2 a
> A 3 z
> Z 3 k
>
> I suspect the solution involves using a pure function of some form as
> the second argument in the Sort function, but I can't figure out what
> this function should be.
>
> Thanks,
> Lee
>


János

2005-06-10, 9:00 am


On Jun 9, 2005, at 5:18 AM, Lee Newman wrote:

> Hello,
>
> I can't come up with a solution to the following simple task:
>
> - I have a matrix, let's M={ {"Z",2,"a"},{"A",2,"k"}, {"Z",1,"z"},
> {"A",1,"a"},{"Z",3,"k"}, {"A",3,"z"} }
> - I want to be able to sort the matrix row-wise, based on multiple
> columns, for example sort first on column 2 (ascending) and then
> secondly on column1 (ascending)
>
> desired result:
> A 1 a
> Z 1 z
> A 2 k
> Z 2 a
> A 3 z
> Z 3 k
>
> I suspect the solution involves using a pure function of some form as
> the second argument in the Sort function, but I can't figure out what
> this function should be.
>
> Thanks,
> Lee
>


The solution to it is in the Help under the Sort examples. Here it
is for this case:

In[1]:=
mm = {{"Z", 2, "a"},
{"A", 2, "k"}, {"Z", 1,
"z"}, {"A", 1, "a"},
{"Z", 3, "k"}, {"A", 3,
"z"}};

In[2]:=
ColumnForm[mm]
Out[2]=
{"Z", 2, "a"}
{"A", 2, "k"}
{"Z", 1, "z"}
{"A", 1, "a"}
{"Z", 3, "k"}
{"A", 3, "z"}

In[15]:=
nn = mm[[Ordering[mm[[All,
{2, 1}]]]]];

In[16]:=
ColumnForm[nn]
Out[16]=
{"A", 1, "a"}
{"Z", 1, "z"}
{"A", 2, "k"}
{"Z", 2, "a"}
{"A", 3, "z"}
{"Z", 3, "k"}

János

Mark Fisher

2005-06-10, 9:00 am

How about this?

ColumnOrderSort[matrix_List, columns_List] :=
Sort[matrix, OrderedQ[{#1[[columns]], #2[[columns]]}] &]

ColumnOrderSort[M, {2,1}]//MatrixForm

--Mark

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com