Re: Custom sort a list of pairs
- To: mathgroup at smc.vnet.net
- Subject: [mg73658] Re: Custom sort a list of pairs
- From: "dimitris" <dimmechan at yahoo.com>
- Date: Sat, 24 Feb 2007 02:11:12 -0500 (EST)
- References: <ermdtt$i3t$1@smc.vnet.net>
Hi. I apologize in advance I didn't understand well! > 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. > 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}} 3, 2,2,1,1,1 indeed sorted by reverse canonical order. a,a,b,a,b,c sorted in normal order? I doubt. Did you really mean something like the following? In[69]:= {{1, a}, {2, a}, {3, a}, {1, b}, {2, b}, {1, c}} Transpose[%] % /. {x_List, y_List} :> {Sort[x, #2 - #1 < 0 & ], Sort[y]} Transpose[%] Out[69]= {{1,a},{2,a},{3,a},{1,b},{2,b},{1,c}} Out[70]= {{1,2,3,1,2,1},{a,a,a,b,b,c}} Out[71]= {{3,2,2,1,1,1},{a,a,a,b,b,c}} Out[72]= {{3,a},{2,a},{2,a},{1,b},{1,b},{1,c}} Dimitris =CF/=C7 planetmarshalluk at hotmail.com =DD=E3=F1=E1=F8=E5: > 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