Re: Friendly Challenge 2: sort by column
- To: mathgroup at smc.vnet.net
- Subject: [mg35020] Re: [mg35005] Friendly Challenge 2: sort by column
- From: Andrzej Kozlowski <andrzej at bekkoame.ne.jp>
- Date: Thu, 20 Jun 2002 02:13:17 -0400 (EDT)
- Sender: owner-wri-mathgroup at wolfram.com
Like last time I will make just a single attempt. In[1]:= f[a_,k_]:=a[[Ordering[Transpose[a][[k]]]]] In[2]:= a = {{77,27,57,53}, {72,21,40,27}, {96,76,87,27}, {61,97,93,71}, {43,80,21,87}}; In[3]:= f[a,3] Out[3]= {{43,80,21,87},{72,21,40,27},{77,27,57,53},{96,76,87,27},{61,97,93,71}} I think this ought to be pretty fast, but I shan't give any explanations preferring to leave this task in the safe hands of Hartmut ;) Andrzej Kozlowski Toyama International University JAPAN http://platon.c.u-tokyo.ac.jp/andrzej/ On Wednesday, June 19, 2002, at 06:52 PM, Mr. Wizard wrote: > 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 received. This is > 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 > > > >