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: [mg73681] Re: [mg73633] Custom sort a list of pairs
  • From: János <janos.lobb at yale.edu>
  • Date: Sat, 24 Feb 2007 02:23:37 -0500 (EST)
  • References: <200702230941.EAA17877@smc.vnet.net>

On Feb 23, 2007, at 4:41 AM, 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
>

Here is a newbie approach:

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

In[2]:=
s = Sort[lst,
    OrderedQ[{#2[[1]],
       #1[[1]]}] & ]
Out[2]=
{{3, a}, {2, a}, {2, b},
   {1, a}, {1, b}, {1, c}}

J=E1nos


------------------------------------------
"The shortest route between two points is the middleman"  Ayn Rand



  • Prev by Date: Re: MatrixRank Modulus->8 - BUG
  • Next by Date: Re: Help with Reduce Command.
  • Previous by thread: Custom sort a list of pairs
  • Next by thread: Re: Custom sort a list of pairs