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: [mg73722] Re: Custom sort a list of pairs
  • From: "Ray Koopman" <koopman at sfu.ca>
  • Date: Sun, 25 Feb 2007 04:43:21 -0500 (EST)
  • References: <ermdtt$i3t$1@smc.vnet.net><erop90$97a$1@smc.vnet.net>

Ray Koopman wrote:
> On Feb 23, 1:59 am, planetmarshal... 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
>
> SeedRandom[2]; items = Table[Random[Integer,4],{20},{2}]
>
> {{4,4},{2,0},{3,3},{4,2},{2,4},{4,0},{4,0},{0,0},{1,4},{2,2},
>  {0,0},{1,2},{2,4},{1,1},{0,3},{1,4},{4,3},{3,2},{4,1},{0,2}}
>
> Flatten[Reverse/@Split[Reverse@Sort@items,#1[[1]]==#2[[1]]&],1]
>
> {{4,0},{4,0},{4,1},{4,2},{4,3},{4,4},{3,2},{3,3},{2,0},{2,2},
>  {2,4},{2,4},{1,1},{1,2},{1,4},{1,4},{0,0},{0,0},{0,2},{0,3}}

Try  Flatten[Reverse@Split[Sort@items,#1[[1]]==#2[[1]]&],1]  instead.



  • Prev by Date: Re: Kernel Crashing with NDSOlve
  • Next by Date: Re: Fuction definition
  • Previous by thread: Re: Custom sort a list of pairs
  • Next by thread: Re: Custom sort a list of pairs