can't sort

Mikael Schönenberg micke at strakt.com
Sat May 17 07:13:53 EDT 2003


On Sat, 17 May 2003, Helmut Jarausch wrote:

> Why does
>
> print [4,1,3,2].sort()
>
> just print
> None
> ?
>
> As far as I understood the docs 'sort'
> should take 'cmp' for comparisons which is
> well defined for integers.

I think it has hit us all. The method sort operates on the list it's
called in, and returns nothing. Your list, however, will be sorted after
you've called it.

>>> foo = [4, 1, 3, 2]
>>> foo
[4, 1, 3, 2]
>>> foo.sort()
>>> foo
[1, 2, 3, 4]
>>> help(foo.sort)
Help on built-in function sort:

sort(...)
    L.sort([cmpfunc]) -- sort *IN PLACE*; if given, cmpfunc(x, y) ->
-1, 0, 1

Hope this helps :)

/micke







More information about the Python-list mailing list