Re: Custom sort a list of pairs
- To: mathgroup at smc.vnet.net
- Subject: [mg73698] Re: Custom sort a list of pairs
- From: Bill Rowe <readnewsciv at sbcglobal.net>
- Date: Sat, 24 Feb 2007 02:32:51 -0500 (EST)
On 2/23/07 at 4:41 AM, planetmarshalluk at hotmail.com wrote:
>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}}
Here is one way to do what you want
In[10]:=
data={{1,a},{2,a},{3,a},{1,b},{2,b},{1,c}};
In[11]:=
Join@@(Reverse[Sort@#]&/@
Split[Sort[data, OrderedQ@{Last@#1, Last@#2}&],
Last@#1 == Last@#2&])
Out[11]=
{{3, a}, {2, a}, {1, a}, {2, b}, {1, b}, {1, c}}
--
To reply via email subtract one hundred and four