2
$\begingroup$

When trying to sort a list of fractions and quadratic numbers I'm trying to work out why sort orders the quadratics after the fractions. It seems to use the function "OrderedQ" and "standard order" or "cannonical order".

Where is this order defined?

Why is

OrderedQ[{1/6 (3 - Sqrt[5]), 2/3}]

False, but

Less[1/6 (3 - Sqrt[5]), 2/3]

is True?

$\endgroup$
1
2
$\begingroup$

First, about documentation. This is very confusing documentation.

OrderedQ says

By default, OrderedQ uses canonical order as described in the notes for Sort.

then going to Sort one sees this

Sort[list,p] applies the ordering function p to pairs of elements in list to determine whether they are in order. The default function p is Order.

Notice that the default is Order. Then going to Order it says

Order uses canonical order as described in the notes for Sort.

It is a circle, where one page sends to another to another and we end up we were started.

But the issue is due to not using numerical input. If you do this

x=1/6 (3-Sqrt[5]);  (* 0.127322 *)
y=2/3;   (* 0.666667 *)
OrderedQ[{x,y}]

Mathematica graphics

But now if we do this

x=1/6 (3.-Sqrt[5]);
y=2/3;
OrderedQ[{x,y}]

Mathematica graphics

And this now matches Less,

Less[x,y]

Mathematica graphics

OrderedQ does not use numerical method to check ordering unless input is numerical. When it is exact, it works as it says in Possible issues

OrderedQ by default works structurally, not by numerical value:

To make it give same value as Less without making it numerical, do

x=1/6 (3-Sqrt[5])
y=2/3;
OrderedQ[{x,y},Less]

Mathematica graphics

So the way you used it, the ordering was not based on numerical value of input. Agree, that this is a bit confusing function to use.

$\endgroup$

Not the answer you're looking for? Browse other questions tagged or ask your own question.