Re: Sort on vector component
- To: mathgroup at smc.vnet.net
- Subject: [mg122007] Re: [mg122006] Sort on vector component
- From: Adriano Pascoletti <adriano.pascoletti at uniud.it>
- Date: Sun, 9 Oct 2011 03:50:24 -0400 (EDT)
- Delivered-to: l-mathgroup@mail-archive0.wolfram.com
- References: <201110080936.FAA21131@smc.vnet.net>
Wolfgang, Mathematica 6 introduced SortBy In[1]:= u = {{2, 12, 7, 21}, {2, 48, 47, 97}, {4, 42, 41, 87}, {6, 8, 1, 15}, {6, 12, 9, 27}}; SortBy[u, #1[[2]] & ] Out[1]= {{6, 8, 1, 15}, {2, 12, 7, 21}, {6, 12, 9, 27}, {4, 42, 41, 87}, {2, 48, 47, 97}} In[2]:= sorter[L_List, i_Integer] := SortBy[L, #1[[i]] & ] In[3]:= sorter[u, 2] Out[3]= {{6, 8, 1, 15}, {2, 12, 7, 21}, {6, 12, 9, 27}, {4, 42, 41, 87}, {2, 48, 47, 97}} In[4]:= sorter[u, 3] Out[4]= {{6, 8, 1, 15}, {2, 12, 7, 21}, {6, 12, 9, 27}, {4, 42, 41, 87}, {2, 48, 47, 97}} In[5]:= sorter[u, 4] Out[5]= {{6, 8, 1, 15}, {2, 12, 7, 21}, {6, 12, 9, 27}, {4, 42, 41, 87}, {2, 48, 47, 97}} In[6]:= sorter[u, 1] Out[6]= {{2, 12, 7, 21}, {2, 48, 47, 97}, {4, 42, 41, 87}, {6, 8, 1, 15}, {6, 12, 9, 27}} Adriano Pascoletti 2011/10/8 Dr. Wolfgang Hintze <weh at snafu.de> > 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)? > > For i=1 it is simply Sort, for the last one I could use > > In[75]:= > Reverse /@ Sort[Reverse /@ u] > Out[75]= > {{6, 8, 1, 15}, {2, 12, 7, 21}, {6, 12, 9, 27}, > {4, 42, 41, 87}, {2, 48, 47, 97}} > > I don't want to go in the details of defining lexicographical order but > just obtain a sorting as described above. > > Any ideas? > > Thanks in advance. > > Wolfgang > > >
- References:
- Sort on vector component
- From: "Dr. Wolfgang Hintze" <weh@snafu.de>
- Sort on vector component