sorting a list numbers stored as strings

Amit Khemka khemkaamit at gmail.com
Tue Sep 25 07:31:14 CEST 2007


On 9/24/07, Carsten Haese <carsten at uniqsys.com> wrote:
> On Mon, 2007-09-24 at 16:53 +0530, Amit Khemka wrote:
> > On 9/24/07, aine_canby at yahoo.com <aine_canby at yahoo.com> wrote:
<snip>
> >
> > >>> l = ["1", "11", "2", "22"]
> > >>> sorted(l, cmp = lambda x, y: cmp(int(x), int(y)))  # provide your
> > own compare function !
> > >>> l
> > ['1', '2', '11', '22']
>
> That interpreter session is a work of fiction, since sorted returns the
> sorted list instead of sorting the list in place.

I am sorry, thanks for pointing out !
What I intended to write was:

>>> l = sorted(l, cmp = lambda x, y: cmp(int(x), int(y)))

Btw, It was more of a goofed up Reality show !

cheers,
-- 
----
Amit Khemka
website: www.onyomo.com
wap-site: www.owap.in



More information about the Python-list mailing list