RE: Friendly Challenge 2: sort by column

• To: mathgroup at smc.vnet.net
• Subject: [mg35024] RE: [mg35005] Friendly Challenge 2: sort by column
• From: "Wolf, Hartmut" <Hartmut.Wolf at t-systems.com>
• Date: Thu, 20 Jun 2002 02:13:24 -0400 (EDT)
• Sender: owner-wri-mathgroup at wolfram.com

```
> -----Original Message-----
> From: Wolf, Hartmut
To: mathgroup at smc.vnet.net
> Sent: Wednesday, June 19, 2002 2:44 PM
> Subject: [mg35024] RE: [mg35005] Friendly Challenge 2: sort by column
>
>
>
> > -----Original Message-----
> > From: gleam at flashmail.com [mailto:gleam at flashmail.com]
To: mathgroup at smc.vnet.net
> > Sent: Wednesday, June 19, 2002 11:53 AM
> > Subject: [mg35024] [mg35005] Friendly Challenge 2: sort by column
> >
> >
> > 	Once again, I am posting a friendly challenge with the hope of
> > provoking thought and encouraging creativity.  This time, the
> > challenge is to write the "best" sort-by-column function you can.
> >
> > 	Once again, have already written several such
> > functions, which I will
> > delay posting these until a number of responses are
> > not because I think I am some kind of Mathematica "guru"
> will all the
> > right answers, but rather to prevent my methods from influencing
> > others.
> >
> > 	Once again, though I am setting guidelines for this
> > challenge, I will
> > not be pronouncing some final judgement of a winning algorithm.  I
> > would much prefer that this challenge induces a good
> discussion of the
> > merits of various approaches and methods.
> >
> > Description:
> >
> > The function shall take two arguments: (1) a rectangular matrix and
> > (1) the number of a column in (1).
> >
> > The function shall sort the rows of (1) according primarily to the
> > element in the column specified by (2).
> >
> > The function shall not effect sub-elements in a three dimensional
> > matrix.
> >
> > Example:
> >
> > a = {{77,27,57,53},
> >      {72,21,40,27},
> >      {96,76,87,27},
> >      {61,97,93,71},
> >      {43,80,21,87}}
> >
> > In:= f[a, 3]
> >
> > Out= {{43,80,21,87},
> >       {72,21,40,27},
> >       {77,27,57,53},
> >       {96,76,87,27},
> >       {61,97,93,71}}
> >
> > You may handle duplicates in the sort column as you see fit, but
> > please explain it for the rest of us, and be prepared give
> support for
> > this as you would all other behaviors of your function.
> >
> > Be creative, have fun, and good luck!
> >
> > Paul
> >
> >
>
> Paul,
>
> a few quick ideas (leave testing up to you), unbundeled, unsorted:
>
> (1)
>
> Transpose[
>
> RotateRight[Transpose[Sort[Transpose[RotateLeft[Transpose[a],
> 2]]]], 2]]
>
>
> (2)
>
> RotateRight[Sort[RotateLeft[a,{0,2}]],{0,2}]
>
>
> (3)
>
> Sort[a, #1[[3]] < #2[[3]] &]
>
>
> (4)
>
> a[[Ordering[Transpose[a][[3]]]]]
>
>
> --
> Hartmut
>