473,544 Members | 420 Online

# sorting a list numbers stored as strings

hi,

I have the following list -

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

how do I sort it like this -

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

thanks,

aine

Sep 24 '07 #1
6 1605
ai********@yaho o.com 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(ite m), item) for item in source)]
print result

Sep 24 '07 #2
On 9/24/07, ai********@yaho o.com <ai********@yah oo.comwrote:
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
Sep 24 '07 #3
On Mon, 2007-09-24 at 16:53 +0530, Amit Khemka wrote:
On 9/24/07, ai********@yaho o.com <ai********@yah oo.comwrote:
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
Sep 24 '07 #4
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(ke y=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
Sep 24 '07 #5
In article <11************ **********@y42g 2000hsy.googleg roups.com>,
ai********@yaho o.com 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
Sep 24 '07 #6
On 9/24/07, Carsten Haese <ca*****@uniqsy s.comwrote:
On Mon, 2007-09-24 at 16:53 +0530, Amit Khemka wrote:
On 9/24/07, ai********@yaho o.com <ai********@yah oo.comwrote:
<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
Sep 25 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.