# Comparison of functions

Steven D'Aprano steve at REMOVETHIScyber.com.au
Sat Jul 30 19:59:40 CEST 2005

```On Sat, 30 Jul 2005 16:13:22 +0000, Adriano Varoli Piazza wrote:

> Steven D'Aprano ha scritto:
>
>> It was easy. I never once asked myself whether some complex number was
>> greater or less than another, I just asked "which one comes first in a
>> lexicographic sort?"
>>
>> The two questions are NOT the same, and it is an ugliness in an otherwise
>> beautiful language that Python treats them as the same.
>>
>> Mathematically, 1 == 1.0 == 1+0j but in the dictionary "1" should sort
>> before "1.0" which sorts before "1.0+0.0j".
>
> Because, of course, when I sort numbers the first thing I think of is
> looking at the ascii table... Here I was, thinking maths was useful.
> Sorting numbers "lexicographically" might make sense to you, but it's
> totally unintuitive to me. For example, why or how would I guess that
> "3-3j" is bigger (or smaller) than "3+3j"?

3-3j is NOT bigger than 3+3j, nor is it smaller. You said so yourself:
"it's not possible to define an order that preserves the properties of
arithmetical operations on complex numbers."

Fortunately, when we sort a list, we don't care about preserving the
properties of arithmetical operations, we just need to place each item in
a known, non-arbitrary (or at least not too arbitrary) position.

> You'll still want to sort complex numbers lexicographically. It'll still
> have no meaning whatsoever, so you might as well leave the list
> unsorted.

Your mathematical purity does you great credit. Unfortunately,
it is also quite impractical. I'll give you a real-world actual case where
sorting complex numbers is important: "The Penguin Dictionary Of
Curious and Interesting Numbers" by David Wells. Some few hundreds of
numbers are listed, including i. Should Wells have listed those numbers in
random unsorted order just to satisfy some purists who insist that placing
i in the list makes sorting the other 399 entries meaningless?

> You might think you sorted something. 100? 200? years of maths
> say you didn't.

Sorting is not just numeric ordering. There are many different collation
systems, not just ordering numbers by their magnitude.

Think about sorting guests around a dinner table: married couples next to
each other, single people across from another single person of the
opposite sex, children at the end of the table, babies next to their
mother, Uncle Enno as far away from Aunt May as possible. It is a
complicated algorithm, but it is still sorting (people, in this case, not
numbers).

numbers, but if you actually studied the mathematics of orderings sets,
you would realise how silly it was to say that a century of mathematics
says that I didn't sort a list. Mathematicians are fully aware that
numeric sorting is just one way of many of sorting.

Do you understand the difference between partial and total ordering, or
weakly and strongly ordered? When you do understand them, come back and
tell me again whether you still think lexicographic sorting has no meaning
whatsoever.

--
Steven.

```