MathGroup Archive 2007

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Custom sort a list of pairs


On 23 Feb., 10:59, 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

Hi Andrew

You can use this:

data = {{1, a}, {2, a}, {3, a}, {1, b}, {2, b}, {1, c}}
Sort[data, ( ! OrderedQ[{#1[[1]], #2[[1]]}] ||  #1[[1]] == #2[[1]])
&&
    OrderedQ[{#1[[2]], #2[[2]]}] & ]

Best Regards
Norbert Marxer

www.mec.li



  • Prev by Date: PolyLog help
  • Next by Date: Re: Help with Reduce Command.
  • Previous by thread: Re: Custom sort a list of pairs
  • Next by thread: Re: Custom sort a list of pairs