In article <[EMAIL PROTECTED]>,
 Terry Reedy <[EMAIL PROTECTED]> wrote:

> Yes, key= lets you sort anything anyway you want.
>  >>> l=[1, '2', 3j]
>  >>> sorted(l, key = str)
> [1, '2', 3j]

The problem here is that str() is degenerate, i.e. a != b does not imply 
str(a) != str(b).

>  >>> sorted(l, key = id)
> ['2', 3j, 1]

And of course, this has to opposite problem, a == b does not imply id(a) == 
id(b).  Whether either of these "problems" is really a problem obviously 
depends on what you're trying to do.

In 3.0, can you still order types?  In 2.x, you can do:

>>> t1 = type(1)
>>> t2 = type(1j)
>>> t1 < t2
False

If this still works in 3.0, then you can easily do something like:

def total_order(o1, o2):
   "Compare any two objects of arbitrary types"
   try:
      return o1 <= o2
   except UncomparableTypesError:   # whatever the right name is
      return type(o1) <= type(o2)

and get the same effect as you had in 2.x.
--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to