Perl-Python-a-Day: Sorting

Fredrik Lundh fredrik at pythonware.com
Sat Oct 15 20:18:11 CEST 2005


Tim Roberts wrote:

> >This entry is obsolete: it should mention the 'key' option of the
> >standard sort method.
>
> It should mention it, but not necessarily recommend it.
>
> I haven't run the numbers in Python, but in Perl, the undecorated sort is
> so well-optimized that the Schwartzian transform is almost always faster
> than passing a custom comparator to the sort function.

the "key" option is used for decoration.  from the documentation:

    key specifies a function of one argument that is used to extract
    a comparison key from each list element: "key=str.lower"

    In general, the key and reverse conversion processes are
    much faster than specifying an equivalent cmp function. This
    is because cmp is called multiple times for each list element
    while key and reverse touch each element only once.

</F>






More information about the Python-list mailing list