473,659 Members | 2,662 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1613
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
in place. Also, you should probably read about the "sorted" function
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.

Similar topics

2
3071
by: D. Roshani | last post by:
Hello ! I wonder if any one can help me to create a cosomize sorting order (as Macro or added small program in c++ or c# which does this work) in a Access Database contaning one table only words encoded in ISO-8859-1 A a B b C c D d E e F f G g H h I i J j 1 2 3 4 5 6 7 8 9 10 11 12 Jh jh K k L l ll M m N n O o P p Q q R r rr S s...
7
3255
by: Federico G. Babelis | last post by:
Hi All: I have this line of code, but the syntax check in VB.NET 2003 and also in VB.NET 2005 Beta 2 shows as unknown: Dim local4 As Byte Fixed(local4 = AddressOf dest(offset)) CType(local4, Short) = CType(src, Short)
22
4137
by: mike | last post by:
If I had a date in the format "01-Jan-05" it does not sort properly with my sort routine: function compareDate(a,b) { var date_a = new Date(a); var date_b = new Date(b); if (date_a < date_b) { return -1; } else
20
4054
by: Xah Lee | last post by:
Sort a List Xah Lee, 200510 In this page, we show how to sort a list in Python & Perl and also discuss some math of sort. To sort a list in Python, use the “sort” method. For example: li=;
7
10080
by: Foodbank | last post by:
Hi everyone. I'm having trouble with this radix sorting program. I've gotten some of it coded except for the actual sorting :( The book I'm teaching myself with (Data Structures Using C and C++) just doesn't explain things good at all and the tutorials I've viewed don't really explain least significant digit first sorting or show examples, which would be most helpful. I've commented the section where I know that the iteration of the...
3
1968
by: melanieab | last post by:
Hi, When you click on a column header to sort in ascending or descending order, numbers (and dates) aren't sorted correctly. It turns up like this: 1 10 11 2 3 4
9
2603
by: Dylan Parry | last post by:
Hi folks, I have a database that contains records with IDs like "H1, H2, H3, ..., Hn" and these refer to local government policy numbers. For example, H1 might be "Housing Policy 1" and so on. For some more insight, not all policies will be prefixed with an "H", an in fact they could be prefixed with *any* letter combination, but they will always end with a number. When I retrieve them from the database I use "ORDER BY id" to get them...
1
7006
by: Mansi Shah | last post by:
Hi All, I have used gridview in my application and i m using dataview to sort the grid. But when i sort the numeric column, dataview sorts it like a string. So, after sorting, instead of getting 1,3,11.... i get 1,11,3... in asc and 3,1,11 in dec.. Anybody has idea regarding this???
5
3179
by: lemlimlee | last post by:
hello, this is the task i need to do: For this task, you are to develop a Java program that allows a user to search or sort an array of numbers using an algorithm that the user chooses. The search algorithms that can be used are Linear Search and Binary Search. The sorting algorithms are bubble, selection and Insertion sort. First, the user is asked whether he/she wants to perform a search option, a sort operation, or exit the program. If...
0
8427
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, well explore What is ONU, What Is Router, ONU & Routers main usage, and What is the difference between ONU and Router. Lets take a closer look ! Part I. Meaning of...
0
8330
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8626
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7355
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing, and deploymentwithout human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6178
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupr who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5649
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4175
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2749
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1975
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.