Why does this work in jython and not in python?

Fredrik Lundh fredrik at pythonware.com
Wed Jun 18 13:46:39 EDT 2003


Roman Milner wrote:

> I'm sorting a list of dictionaries based on a particular key. Using
> the code below, the list gets sorted in jython, but not in python.
> I'm using python 2.2.1 and jython 2.1.
>
> I really appreciate any help.

a careful look at the "sort" documentation might help:

    http://www.python.org/doc/current/lib/typesseq-mutable.html

    The sort() method takes an optional argument specifying a
    comparison function of two arguments (list items) which should
    return a negative, zero or positive number depending on whether
    the first argument is considered smaller than, equal to, or larger
    than the second argument.

"a < b" doesn't do that, obviously.

the easiest way to get a correct number is to use the "cmp"
function:

def sort_func(one, two):
    return cmp(one['a'], two['a'])

</F>








More information about the Python-list mailing list