Velocity Reviews > sorting a list numbers stored as strings

# sorting a list numbers stored as strings

aine_canby@yahoo.com
Guest
Posts: n/a

 09-24-2007
hi,

I have the following list -

["1", "11", "2", "22"]

how do I sort it like this -

["1", "2", "11", "22"]

thanks,

aine

Bruno Desthuilliers
Guest
Posts: n/a

 09-24-2007
http://www.velocityreviews.com/forums/(E-Mail Removed) a écrit :
> hi,
>
> I have the following list -
>
> ["1", "11", "2", "22"]
>
> how do I sort it like this -
>
> ["1", "2", "11", "22"]

source = ["1", "11", "2", "22"]
result = [t[1] for t in sorted((int(item), item) for item in source)]
print result

Amit Khemka
Guest
Posts: n/a

 09-24-2007
On 9/24/07, (E-Mail Removed) <(E-Mail Removed)> wrote:
> hi,
>
> I have the following list -
>
> ["1", "11", "2", "22"]
>
> how do I sort it like this -
>
> ["1", "2", "11", "22"]
>

Hi,

>>> 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']

Cheers,

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

Carsten Haese
Guest
Posts: n/a

 09-24-2007
On Mon, 2007-09-24 at 16:53 +0530, Amit Khemka wrote:
> On 9/24/07, (E-Mail Removed) <(E-Mail Removed)> wrote:
> > hi,
> >
> > I have the following list -
> >
> > ["1", "11", "2", "22"]
> >
> > how do I sort it like this -
> >
> > ["1", "2", "11", "22"]
> >

>
> Hi,
>
> >>> 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. Also, it's better
(i.e. more readable and likely faster) to use a sort key function
instead of a comparison function whenever possible. In this case, the
sort key function is particularly trivial:

>>> l = ["1", "11", "2", "22"]
>>> sorted(l, key=int)

['1', '2', '11', '22']

--
Carsten Haese
http://informixdb.sourceforge.net

Peter Otten
Guest
Posts: n/a

 09-24-2007
aine_canby wrote:

> I have the following list -
>
> ["1", "11", "2", "22"]
>
> how do I sort it like this -
>
> ["1", "2", "11", "22"]

>>> items = ["1", "11", "2", "22"]
>>> items.sort(key=int)
>>> items

['1', '2', '11', '22']

This is more efficient than Amit's compare function and even Bruno's
decorate-sort-undecorate (DSU) -- which of course only matters if the list
becomes a bit larger.

Peter

Michael J. Fromberger
Guest
Posts: n/a

 09-24-2007
In article <(E-Mail Removed) .com>,
(E-Mail Removed) wrote:

> hi,
>
> I have the following list -
>
> ["1", "11", "2", "22"]
>
> how do I sort it like this -
>
> ["1", "2", "11", "22"]
>
> thanks,
>
> aine

Try:

lst.sort(key = lambda s: int(s))

Assuming, of course, that "lst" is your original list; this will sort it
here:

<http://docs.python.org/lib/built-in-funcs.html>

Cheers,
-M

--
Michael J. Fromberger | Lecturer, Dept. of Computer Science
http://www.dartmouth.edu/~sting/ | Dartmouth College, Hanover, NH, USA

Amit Khemka
Guest
Posts: n/a

 09-25-2007
On 9/24/07, Carsten Haese <(E-Mail Removed)> wrote:
> On Mon, 2007-09-24 at 16:53 +0530, Amit Khemka wrote:
> > On 9/24/07, (E-Mail Removed) <(E-Mail Removed)> 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