Re: Sort on vector component
- To: mathgroup at smc.vnet.net
- Subject: [mg122024] Re: Sort on vector component
- From: Bill Rowe <readnews at sbcglobal.net>
- Date: Sun, 9 Oct 2011 03:53:28 -0400 (EDT)
- Delivered-to: l-mathgroup@mail-archive0.wolfram.com
On 10/8/11 at 5:36 AM, weh at snafu.de (Dr. Wolfgang Hintze) wrote: >Given a list >u = {{2, 12, 7, 21}, {2, 48, 47, 97}, {4, 42, 41, 87}, {6, 8, 1, >15}, {6, 12, 9, 27}} >How can I sort it with respect to a given index position i=(1, ..., >4)? One way to do this that should be fairly fast using index 3 as an example is: In[2]:= u[[Ordering[u[[All, 3]]]]] Out[2]= {{6, 8, 1, 15}, {2, 12, 7, 21}, {6, 12, 9, 27}, {4, 42, 41, 87}, {2, 48, 47, 97}} Another way would be to use a pure function as the ordering function with Sort. That is: In[5]:= Sort[u, #1[[3]] < #2[[3]] &] Out[5]= {{6, 8, 1, 15}, {2, 12, 7, 21}, {6, 12, 9, 27}, {4, 42, 41, 87}, {2, 48, 47, 97}}