Services & Resources / Wolfram Forums / MathGroup Archive
-----

MathGroup Archive 2007

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

Search the Archive

Re: Sort problem

  • To: mathgroup at smc.vnet.net
  • Subject: [mg81722] Re: Sort problem
  • From: mcmcclur at unca.edu
  • Date: Tue, 2 Oct 2007 05:38:13 -0400 (EDT)
  • References: <fdqd33$n6m$1@smc.vnet.net>

On Oct 1, 5:02 am, Donald DuBois <don... at comcast.net> wrote:
> This Sort works
> In[32]:= Sort[{r, a, x}]
> Out[32]= {a, r, x}
>
> but this one doesn't:
> In[33]:= Sort[{{r, 2}, {a, 3}, {x, 4}}, #1[[1]] < #2[[1]] &]
> Out[33]= {{r, 2}, {a, 3}, {x, 4}}


According to the documentation, the default ordering function
for Sort is OrderedQ[{#1,#2}]& (as opposed to Less, which you use).
Thus, to emulate this behavior with the restriction to the first
element, you could use:
Sort[{{r, 2}, {a, 3}, {x, 4}},
 OrderedQ[{#1[[1]], #2[[1]]}] &]


Conversely, you could force the more confusing behavior in the
first example by switching the input to
Sort[{r, a, x}, #1 < #2 &]

I suppose the reason is that Less compares numerical quantities,
while OrderedQ is more general.

Mark



  • Prev by Date: Re: Other bug in ContourPlot
  • Next by Date: Re: Equivalent functionality to colorbar in Mathematica?
  • Previous by thread: Re: Sort problem
  • Next by thread: Re: Sort problem