MathGroup Archive 1997

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Sorting tables by columns

  • To: mathgroup at smc.vnet.net
  • Subject: [mg6402] Re: [mg6385] Sorting tables by columns
  • From: "w.meeussen" <w.meeussen.vdmcc at vandemoortele.be>
  • Date: Sun, 16 Mar 1997 19:25:16 -0500 (EST)
  • Sender: owner-wri-mathgroup at wolfram.com

At 01:21 15-03-97 -0500, fbd2 at psu.edu wrote:
>Hi,
>
>Does anyone know how to sort tables by one of its columns? 
>For example,  the table
>
>2  3  4  5
>1  2  7  8
>6  9  2  1
>
>can be sorted by column 1, giving:
>
>1  2  7  8
>2  3  4  5
>6  9  2  1
>
>This can be done using Excel; however I need to perform
>this operation inside a Mathematica program.
>Is there an automated procedure in Mathematica to accomplish this?
>
>Thanks,
>
>Flavio
>
>
>
hello,

as I see it, you are supposed to write them 'on the fly',
using tools as found in the help file under 'Sort'.
Something along the lines of:

In[1]:=
it={{2,  3,  4 , 5},
{1 , 2 , 7,  8},
{6 , 9  ,2 , 1}}
Out[1]=
{{2,3,4,5},{1,2,7,8},{6,9,2,1}}

In[2]:=
Sort at it
Out[2]=
{{1,2,7,8},{2,3,4,5},{6,9,2,1}}

In[3]:=
Sort[it,OrderedQ[{#1[[4]], #2[[4]]}]&]
Out[3]=
{{6,9,2,1},{2,3,4,5},{1,2,7,8}}

In[4]:=
Sort[it,#1[[4]]> #2[[4]]&]
Out[4]=
{{1,2,7,8},{2,3,4,5},{6,9,2,1}}

In[5]:=
Dimensions[it]
Out[5]=
{3,4}

In[6]:=
mytablesort[
		m_?MatrixQ,
		col_Integer/;col<=Dimensions[it][[2]]]:=
        Sort[m,#1[[col]]> #2[[col]]&         ]

In[7]:=
mytablesort[it,3]
Out[7]=
{{1,2,7,8},{2,3,4,5},{6,9,2,1}}

In[8]:=
Remove[mytablesort]

extension to 3 and more dimensions get more tricky.

bye,

Wouter

Dr. Wouter L. J. MEEUSSEN
eu000949 at pophost.eunet.be
w.meeussen.vdmcc at vandemoortele.be



  • Prev by Date: Re: Problems with TimeConstrained in Win3.1
  • Next by Date: Why does TimeUsed die?
  • Previous by thread: Re: Sorting tables by columns
  • Next by thread: Re: Sorting tables by columns