MathGroup Archive 2000

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

Search the Archive

Re: newbie question on sorting

  • To: mathgroup at smc.vnet.net
  • Subject: [mg26446] Re: [mg26420] newbie question on sorting
  • From: Ken Levasseur <Kenneth_Levasseur at uml.edu>
  • Date: Thu, 21 Dec 2000 01:51:47 -0500 (EST)
  • References: <200012200521.AAA05601@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

niels:

Sort will automatically sort according to the first column since the standard
lexicographic ordering of the rows would start there.   For example, if you
start with this simple example:
In[1]:=
M = Table[Random[Integer, {0, 100}], {4}, {4}]

Out[1]=
{{23, 86, 53, 4}, {43, 24, 66, 23}, {98, 44, 33, 33}, {90, 50, 58, 14}}

You can see that the sorting is done as you desire.
In[2]:=
Sort[M]

Out[2]=
{{23, 86, 53, 4}, {43, 24, 66, 23}, {90, 50, 58, 14}, {98, 44, 33, 33}}

What's probably less obvious is how you would sort by some other column.  To
do that you can add a second argument to Sort which is a pure function of two
variable.

In[3]:=
Sort[M, #1[[2]] <= #2[[2]] & ]

Out[3]=
{{43, 24, 66, 23}, {98, 44, 33, 33}, {90, 50, 58, 14},  {23, 86, 53, 4}}


Ken Levasseur
Mathematical Sciences
UMass Lowell

Three credit on-line Introduction to Mathematica starts at the end of
January:  http://faculty.uml.edu/klevasseur/courses/m419/m419.html




niels Waller wrote:

> Dear mathgroup readers,
>
> I have just started using Mathematica for a programming task and have run
> into a difficulty.  I have a matrix M. I would like to sort the rows of M
> based on the values of the first column.  Is there any easy way to do this?
> Any and all help will be greatly appreciated.
>
> Thanks,
>
> Niels
>
> Please send suggestions to: niels.waller at vanderbilt.edu
>
> *******************************************************
> Niels Waller, Ph.D.
> Director of Quantitative Methods and Evaluation
> Department of Psychology and Human Development
> Vanderbilt University
> Phone: (615) 322-8380    email: niels.waller at vanderbilt.edu
> QME Home page: http://www.vanderbilt.edu/quantmetheval/
> *******************************************************



  • Prev by Date: Re: Rotation of 2D graphics
  • Next by Date: RE: Rotation of 2D graphics
  • Previous by thread: newbie question on sorting
  • Next by thread: Re: newbie question on sorting