Re: Re: Strange behavior of Sort
- To: mathgroup at smc.vnet.net
- Subject: [mg12989] Re: Re: [mg12941] Strange behavior of Sort
- From: BobHanlon at aol.com
- Date: Sun, 28 Jun 1998 02:52:12 -0400
- Sender: owner-wri-mathgroup at wolfram.com
Edward,
The Sort order is given in section A.3.9:
A.3.9 Ordering of Expressions
The canonical ordering of expressions used automatically with the
attribute Orderless and in functions such as Sort satisfies the
following rules:
Integers, rational and approximate real numbers are ordered by
their numerical values.
Complex numbers are ordered by their real parts, and in the event of
a tie, by the absolute values of their imaginary parts.
Symbols are ordered according to their names, and in the event of a tie,
by their contexts.
Expressions are usually ordered by comparing their parts in a
depth-first manner. Shorter expressions come first. Powers
and products are treated specially, and are ordered to correspond
to terms in a polynomial.
Strings are ordered as they would be in a dictionary, with the
upper-case versions of letters coming after lower-case ones.
Ordinary letters
appear first, followed in order by script, Gothic, double-struck,
Greek and Hebrew. Mathematical operators appear in order of
decreasing precedence.
Note that sorting by numerical value only applies to integers, rational,
and approximate real numbers.
Sort follows these rules within each of the categories; however, the
rules do not state the order between the individual categories, so
that must be inferred from Sort's behavior. Note also that numeric
constants such as Pi are symbols until their numeric properties are
either explicitly (e.g., N[Pi]) or implicitly invoked (e.g., 1.7 *
Pi).
The FullForm of your sorted list is
List[1,Power[2,Rational[-1,2]],Times[-1,Power[2,Rational[1,2]]],a,c]
It is sorted consistently with the given rules.
Bob Hanlon
In a message dated 6/24/98 11:23:38 PM, you wrote:
>BobHanlon at aol.com wrote:
>
>> Edward,
>>
>> While the list is numeric, it is not composed of numbers. Provide the
sort
>> criteria that you want:
>>
>> Sort[li, N[#1] < N[#2] & ]
>>
>> Bob Hanlon
>>
>> In a message dated 6/24/98 7:52:22 AM, edneuman at siu.edu wrote:
>>
>> >In Mathematica 3.0:
>> >
>> >In[5]:=
>> >li={1/Sqrt[2], 1, -Sqrt[2]};
>> >
>> >In[7]:=
>> >Sort[li]//InputForm
>> >
>> >Out[7]//InputForm=
>> >{1, 1/Sqrt[2], -Sqrt[2]}
>> >
>> >Sort doesn't work properly on this list.
>
> Bob;
>
>Using your argument how would you explain this:
>
>In[5]:=
>Sort[ {1/Sqrt[2], 1, -Sqrt[2], c, a} ]//InputForm
>
>Out[5]//InputForm=
>{1, 1/Sqrt[2], -Sqrt[2], a, c}
>
>In[6]:=
>NumberQ[a]
>
>Out[6]=
>False
>
>c and a are not numbers, however Sort did a job sorting properly tail of the
>list li.