Re: Custom sort a list of pairs
- To: mathgroup at smc.vnet.net
- Subject: [mg73681] Re: [mg73633] Custom sort a list of pairs
- From: János <janos.lobb at yale.edu>
- Date: Sat, 24 Feb 2007 02:23:37 -0500 (EST)
- References: <200702230941.EAA17877@smc.vnet.net>
On Feb 23, 2007, at 4:41 AM, planetmarshalluk at hotmail.com wrote: > Hi there, > > I've searched through existing posts to no avail, what I want to do is > this: > Sort a nested list of pairs, so that each pair is sorted by reverse > canonical order on its first element, and in normal order on its > second element. > > For example, I want some function > > sortPairs[items_] > > that when given the input > > {{ 1,a},{2,a},{3,a},{1,b},{2,b},{1,c}} > > produces the output > > {{3,a},{2,a},{2,b}{1,a},{1,b},{1,c}} > > Any help much appreciated. > > Thanks, > Andrew > Here is a newbie approach: In[1]:= lst = {{1, a}, {2, a}, {3, a}, {1, b}, {2, b}, {1, c}}; In[2]:= s = Sort[lst, OrderedQ[{#2[[1]], #1[[1]]}] & ] Out[2]= {{3, a}, {2, a}, {2, b}, {1, a}, {1, b}, {1, c}} J=E1nos ------------------------------------------ "The shortest route between two points is the middleman" Ayn Rand
- References:
- Custom sort a list of pairs
- From: planetmarshalluk@hotmail.com
- Custom sort a list of pairs