30

10

This answer in which I wrote about the Operator Precedence Table was unexpectedly popular.

That got me thinking about similar things, and I am wondering:

**Does an "ordering table" exist for the canonical order used by Sort and similar functions?**

(

`Ordering`

, `Order`

, `OrderedQ`

)It's easy enough to test the ordering of various expressions, but just as the precedence table contains some surprises even for experienced users I'm wondering if the canonical ordering does too? It seems odd if this isn't spelled out clearly somewhere, yet I don't recall seeing such a list.

The documentation for `Order`

tantalizingly says:

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

Yet the documentation for `Sort`

is quite basic, as far as I can find, saying only:

- Sort by default orders integers, rational, and approximate real numbers by their numerical values.
- Sort orders complex numbers by their real parts, and in the event of a tie, by the absolute values of their imaginary parts.
- Sort orders symbols by their names, and in the event of a tie, by their contexts.
- Sort usually orders expressions by putting shorter ones first, and then comparing parts in a depth-first manner.
- Sort treats powers and products specially, ordering them to correspond to terms in a polynomial.
- Sort orders strings as in a dictionary, with uppercase versions of letters coming after lowercase ones. Sort places ordinary letters first, followed in order by script, Gothic, double-struck, Greek, and Hebrew. Mathematical operators appear in order of decreasing precedence.

This describes how `Sort`

treats certain classes of expressions but it doesn't describe the relative ordering of these classes or attempt in any way to be exhaustive.

One consequence of canonical ordering is that e.g. symbols and strings are not sorted intuitively. In canonical ordering the order is

`{a1, a10, a2, a9}`

while intuitive sorting gives`{a1, a2, a9, a10}`

. This comes handy when sorting files for example. – István Zachar – 2013-08-28T12:56:13.4132

Furthermore, the last point is either confusing or downright wrong:

– István Zachar – 2013-08-28T12:58:57.643`Sort@{"a", "A", "á", "Á"}`

yields`{"a", "á", "A", "Á"}`

, which seems to imply thatMathematicacannot really distinguish accented characters (as it was already discussed here).@IstvánZachar yes the first point is a typical computation problem - the intuitive sorting - more well known as natural ordering has to be done in another manner (see http://mathematica.stackexchange.com/questions/10619/sort-strings-by-natural-ordering). That last point on accented character is indeed quite a surprise!

– Jonie – 2013-08-28T14:03:48.260