sorting a list numbers stored as strings
Peter Otten
__peter__ at web.de
Mon Sep 24 08:01:09 EDT 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
More information about the Python-list
mailing list