MathGroup Archive 2007

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Custom sort a list of pairs


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


  • Prev by Date: Re: Custom sort a list of pairs
  • Next by Date: Integral question
  • Previous by thread: Re: Custom sort a list of pairs
  • Next by thread: Re: Custom sort a list of pairs