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: [mg73699] Re: Custom sort a list of pairs
  • From: "Drago Ganic" <dganic at vodatel.net>
  • Date: Sun, 25 Feb 2007 04:30:58 -0500 (EST)
  • References: <ermdtt$i3t$1@smc.vnet.net>

Hi,

try this:

sortPairs[items_] :=
Sort[items, (OrderedQ[{#2[[1]], #1[[1]]}] && OrderedQ[{#1[[2]], #2[[2]]}]
&)]

Your example:

sortPairs[{{ 1, a}, {2, a}, {3, a}, {1, b}, {2, b}, {1, c}}]
{{3, a}, {2, a}, {2, b}, {1, a}, {1, b}, {1, c}}



Greetings from Croatia,
Drago Ganic



<planetmarshalluk at hotmail.com> wrote in message 
news:ermdtt$i3t$1 at smc.vnet.net...
> 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
>
> 




  • Prev by Date: Re: Diferent solution of integral in versions 4 and 5...
  • Next by Date: deleting a title or subtitle in a notebook
  • Previous by thread: Re: Custom sort a list of pairs
  • Next by thread: Re: Custom sort a list of pairs