Re: Sort with -Infinity fails
- To: mathgroup at smc.vnet.net
- Subject: [mg73856] Re: Sort with -Infinity fails
- From: "dimitris" <dimmechan at yahoo.com>
- Date: Fri, 2 Mar 2007 06:28:51 -0500 (EST)
- References: <es6d0l$rmr$1@smc.vnet.net>
Read carefully the documentation for Sort
(*execute the following command*)
In[85]:=
FrontEndExecute[{HelpBrowserLookup["RefGuide", "Sort"]}]
There we read among other things...
Sort[list, p] applies the function p to pairs of elements in list to
determine whether they are in order.
The default function p is OrderedQ[{#1, #2}]&.
So for example
In[86]:=
Sort[{-1, 3, -4, 5}]
Out[86]=
{-4, -1, 3, 5}
is identical to
In[90]:=
Sort[{-1, 3, -4, 5}, OrderedQ[{#1, #2}] & ]
Out[90]=
{-4, -1, 3, 5}
Using Trace you can see the process of sorting
In[91]:=
Trace[Sort[{-1, 3, -4, 5}, OrderedQ[{#1, #2}] & ]]
(*omitting Output*)
Now
In[92]:=
-Infinity < -8 < 8 < Infinity
Out[92]=
True
But
In[93]:=
OrderedQ[{-Infinity, -8}]
Out[93]=
False
So in view of all these, you can understand now the following output
In[94]:=
Sort[{-Infinity, -5, -8, 9, Infinity}]
Out[94]=
{-8, -5, 9, -Infinity, Infinity}
If you want you can use again Trace
In[96]:=
Trace[Sort[{-Infinity,-5,-8,9,Infinity},OrderedQ[{#1,#2}]&]]
(*omitting Output*)
Why now OrderedQ works in this way is beyond my knowledge.
Reading in the Section A.3.9 of Mathematica Book
(*execute the following command to go there!)
In[97]:=
FrontEndExecute[{HelpBrowserLookup["MainBook", "A.3.9"]}]
about the canonical ordering I was not able to figure out the reason.
Anyway...I think you want "the way to be done" and not "why must be
done in this way"...
So...
Your desirable result can be achieved as follows
In[98]:=
Sort[{-Infinity, -5, -8, 9, Infinity}, #1 < #2 & ]
Out[98]=
{-Infinity, -8, -5, 9, Infinity}
or
In[100]:=
Sort[{-Infinity, -5, -8, 9, Infinity}, #2 > #1 & ]
Out[100]=
{-Infinity, -8, -5, 9, Infinity}
=CE=9F/=CE=97 Tom Aldenberg =CE=AD=CE=B3=CF=81=CE=B1=CF=88=CE=B5:
> Dear MathGroup,
>
> Minus Infinity (-Infinity) is smaller than -8, but Sort does not sort it =
as
> expected.
> Is there a Real minus Infinity?
>
>
> In[75]:=
> -Infinity < -8
> Out[75]=
> True
>
> In[76]:=
> Sort[{-Infinity, -8, 5}]
> Out[76]=
> {-8, 5, -=E2=88=9E}
>
>
> Regards,
>
> Tom Aldenberg
>
> _________________________________________________________________________=
___
>
> DISCLAIMER: http://www.rivm.nl/disclaimer.htm