Re: Custom sort a list of pairs

• To: mathgroup at smc.vnet.net
• Subject: [mg73688] Re: Custom sort a list of pairs
• From: Jean-Marc Gulliet <jeanmarc.gulliet at gmail.com>
• Date: Sat, 24 Feb 2007 02:27:25 -0500 (EST)
• Organization: The Open University, Milton Keynes, UK
• References: <ermdtt\$i3t\$1@smc.vnet.net>

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

Hi Andrew,

You could try the following function:

In[1]:=
mySort[lst_] := Module[{iml},
iml = Split[Sort[lst, #1[[1]] > #2[[1]] & ], #1[[1]] == #2[[1]] & ];
{iml, (Ordering[#1[[All,2]]] & ) /@ iml}], 1]]

In[2]:=
mySort[{{1, a}, {2, a}, {3, a}, {1, b}, {2, b}, {1, c}}]

Out[2]=
{{3, a}, {2, a}, {2, b}, {1, a}, {1, b}, {1, c}}

Regards,
Jean-Marc

```

• Prev by Date: Re: split again
• Next by Date: Re: Custom sort a list of pairs
• Previous by thread: Re: Custom sort a list of pairs
• Next by thread: Re: Custom sort a list of pairs