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

Bob,
just a small remark. In sorting we should always use OrderedQ instead of 
Less. Canonical ordering (OrderedQ) is for Mathematica expressions, and 
mathematical ordering (Less) is for real numbers.

Here is an example with complex numbers:
In[1]:=OrderedQ[{2I, I}]
Out[1]=False

In[2]:=2I < I
Less::nord: Invalid comparison with 2I attempted.
Out[2]=2I < I

Greetings from Croatia,
Drago Ganic


"Bob Hanlon" <hanlonr at cox.net> wrote in message 
news:eroqjd$9jf$1 at smc.vnet.net...
> sortPairs[items_]:=Sort[Sort[data,#1[[2]]<#2[[2]]&],
>      #1[[1]]>=#2[[1]]&];
>
> 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}}
>
>
> Bob Hanlon
>
> ---- 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
>
> 




  • Prev by Date: Re: Fuction definition
  • Next by Date: Re: Integral question 4
  • Previous by thread: Re: Custom sort a list of pairs
  • Next by thread: Fuction definition