Re: Re: Sorting Rows of a Matrix

• To: mathgroup at smc.vnet.net
• Subject: [mg65723] Re: [mg65122] Re: [mg65089] Sorting Rows of a Matrix
• From: János <janos.lobb at yale.edu>
• Date: Sun, 16 Apr 2006 03:48:48 -0400 (EDT)
• References: <20060314130602.JZDI9108.eastrmmtao06.cox.net@[172.18.52.8]> <200603151128.GAA23209@smc.vnet.net>
• Sender: owner-wri-mathgroup at wolfram.com

```[This post has been delayed due to email problems - moderator]

Same thing.

In[2]:=
m = Table[{Random[Real,
{-1., 1.}],
{"January", "February",
"March", "April",
"May", "June", "July",
"August", "September",
"October", "November",
"December"}[[Random[
Integer, {1, 12}]]]},
{i, 12}]
Out[2]=
{{0.8701862662632052,
"December"},
{-0.5521628730863237,
"February"},
{0.8807487623337678,
"April"},
{-0.5277191409968145,
"March"},
{-0.7681118969431135,
"September"},
{-0.8802613574843271,
"December"},
{-0.41108085598242217,
"July"},
{0.6057163989012162,
"September"},
{0.04548184603751504,
"September"},
{0.537936460785527,
"September"},
{0.7041469273942609,
"October"},
{0.9587663059986569,
"September"}}

In[6]:=
m[[Ordering[m[[All,{2, 1}]]]]]
Out[6]=
{{0.8807487623337678,
"April"},
{-0.8802613574843271,
"December"},
{0.8701862662632052,
"December"},
{-0.5521628730863237,
"February"},
{-0.41108085598242217,
"July"},
{-0.5277191409968145,
"March"},
{0.7041469273942609,
"October"},
{-0.7681118969431135,
"September"},
{0.04548184603751504,
"September"},
{0.537936460785527,
"September"},
{0.6057163989012162,
"September"},
{0.9587663059986569,
"September"}}

In[7]:=
MatrixForm[%]
Out[7]//MatrixForm=
MatrixForm[
{{0.8807487623337678,
"April"},
{-0.8802613574843271,
"December"},
{0.8701862662632052,
"December"},
{-0.5521628730863237,
"February"},
{-0.41108085598242217,
"July"},
{-0.5277191409968145,
"March"},
{0.7041469273942609,
"October"},
{-0.7681118969431135,
"September"},
{0.04548184603751504,
"September"},
{0.537936460785527,
"September"},
{0.6057163989012162,
"September"},
{0.9587663059986569,
"September"}}]

János

P.S.  If you want to show the resemblance of the ordering of the
calendar months, then you can use "aJanuary", "bFebruary",
"cMarch",.... for the name of the months.

On Mar 15, 2006, at 6:28 AM, Gregory Lypny wrote:

>
> Works like a charm.  Now what if that second column contained strings
> rather than numbers, for example, "Sydney", "Martha", "Alberta", and
> "Ralph"?
>
> Regards,
>
> 	Greg
>
> On Tue, Mar 14, 2006, at 8:05 AM, Bob Hanlon wrote:
>
>> Sort[data, #1[[2]] < #2[[2]] &]
>>
>>
>> Bob Hanlon
>>
>>>
>>> From: Gregory Lypny <gregory.lypny at videotron.ca>
To: mathgroup at smc.vnet.net
>>> Subject: [mg65723] [mg65122] [mg65089] Sorting Rows of a Matrix
>>>
>>> Hello everyone,
>>>
>>> How can I sort the rows of a matrix by using one or more of the
>>> columns as sort keys just like in a spreadsheet?  For example, if
>>> matrix M is 12 x 2 with numbers in the first column and unordered
>>> month numbers in the second, how would I order the rows by month?
>>>
>>> 	Regards,
>>>
>>> 		Greg
>>>
>>>

----------------------------------------------
Trying to argue with a politician is like lifting up the head of a
corpse.
(S. Lem: His Master Voice)

```

• Prev by Date: Re: Mathematica and Education
• Next by Date: Re: Mathematica and Education
• Previous by thread: Re: Re: Re: Re: Mathematica and Education
• Next by thread: [Fwd: new sequences derived in Mathematica this morning from Bob Hanlon's polynomial]