Re: Custom sort a list of pairs

• To: mathgroup at smc.vnet.net
• Subject: [mg73668] Re: Custom sort a list of pairs
• From: "Ray Koopman" <koopman at sfu.ca>
• Date: Sat, 24 Feb 2007 02:16:37 -0500 (EST)
• References: <ermdtt\$i3t\$1@smc.vnet.net>

```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}}

```

• Prev by Date: Integral question
• Next by Date: Re: MatrixRank Modulus->8 - BUG
• Previous by thread: Re: Custom sort a list of pairs
• Next by thread: Re: Custom sort a list of pairs