Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
2007
*January
*February
*March
*April
*May
*June
*July
*August
*September
*October
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 2007

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

Search the Archive

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


  • 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