Regarding sort()

Chris Rebert clp2 at rebertia.com
Mon May 25 04:15:26 EDT 2009


On Mon, May 25, 2009 at 12:51 AM, Jaime Fernandez del Rio
<jaime.frio at gmail.com> wrote:
> Hi Dhananjay,
>
> Sort has several optional arguments, the function's signature is as follows:
>
> s.sort([cmp[, key[, reverse]]])
>
> If you store your data as a list of lists, to sort by the third column
> you could do something like:
>
> data.sort(None, lambda x : x[2])
>
> For more complex sortings, as the one you propose, you need to define
> a compare function, that determines if an object preceeds another or
> not. In your case, something like:

Erm, using a compare function rather than a key function slows down
sorting /significantly/. In fact, the `cmp` parameter to list.sort()
has been *removed* in Python 3.0 because of this.

Also, top-posting is evil. Please avoid it in the future.

Cheers,
Chris
-- 
http://blog.rebertia.com



More information about the Python-list mailing list