473,395 Members | 2,253 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Clever way of sorting strings containing integers?

I tried to do this elegantly, but did not come up with a good solution

Sort strings like
foo1bar2
foo10bar10
foo2bar3
foo10bar2

So that they come out:
foo1bar2
foo2bar3
foo10bar2
foo10bar10

I.e. isolate integer parts and sort them according to integer value.

Thx
Holger
Oct 9 '08 #1
4 2416
On 9 Okt., 09:41, Holger <ish...@gmail.comwrote:
I tried to do this elegantly, but did not come up with a good solution

Sort strings like
foo1bar2
foo10bar10
foo2bar3
foo10bar2

So that they come out:
foo1bar2
foo2bar3
foo10bar2
foo10bar10

I.e. isolate integer parts and sort them according to integer value.

Thx
Holger
or even:
foo1bar2
foo10bar10
foo2bar3
foo10bar2
fo
bar1000
777

To:
777
bar1000
fo
foo1bar2
foo2bar3
foo10bar2
foo10bar10

Here's my own take, but it doesn't quite work yet:
txtline = re.compile(r'(\D*)(\d+)')

lines = [x.strip() for x in sys.stdin.readlines()]
res = []
for l in [ x for x in lines if x]:
groups = txtline.findall(l)
res.append([[[x[0], int(x[1],0)] for x in groups],l])

res.sort()
for x in res:
print x[1]
Oct 9 '08 #2
On Thu, 09 Oct 2008 00:41:27 -0700, Holger wrote:
I tried to do this elegantly, but did not come up with a good solution

Sort strings like
foo1bar2
foo10bar10
foo2bar3
foo10bar2

So that they come out:
foo1bar2
foo2bar3
foo10bar2
foo10bar10

I.e. isolate integer parts and sort them according to integer value.
import re
def key_func(string):
result = re.split(r'(\d+)', string)
for i in xrange(1, len(result), 2):
result[i] = int(result[i])
return result
def main():
lines = ['foo1bar2',
'foo10bar10',
'foo2bar3',
'foo10bar2',
'fo',
'bar1000',
'777']
lines.sort(key=key_func)
print '\n'.join(lines)
Ciao,
Marc 'BlackJack' Rintsch
Oct 9 '08 #3
On 9 Ott, 09:41, Holger <ish...@gmail.comwrote:
I tried to do this elegantly, but did not come up with a good solution

Sort strings like
foo1bar2
foo10bar10
foo2bar3
foo10bar2

So that they come out:
foo1bar2
foo2bar3
foo10bar2
foo10bar10

I.e. isolate integer parts and sort them according to integer value.

Thx
Holger
This should work, if you have all stribngs in memory:

import re
REXP = re.compile( r'\d+' )
lines = ['foo1bar2', 'foo10bar10', 'foo2bar3', 'foo10bar2' ]
def key_function( s ): return map(int, re.findall(REXP, s ))
lines.sort( key=key_function)
Ciao
----
FB
Oct 9 '08 #4
On 9 Okt., 10:57, Marc 'BlackJack' Rintsch <bj_...@gmx.netwrote:
On Thu, 09 Oct 2008 00:41:27 -0700, Holger wrote:
I tried to do this elegantly, but did not come up with a good solution
Sort strings like
foo1bar2
foo10bar10
foo2bar3
foo10bar2
So that they come out:
foo1bar2
foo2bar3
foo10bar2
foo10bar10
I.e. isolate integer parts and sort them according to integer value.

import re

def key_func(string):
* * result = re.split(r'(\d+)', string)
* * for i in xrange(1, len(result), 2):
* * * * result[i] = int(result[i])
* * return result

def main():
* * lines = ['foo1bar2',
* * * * * * *'foo10bar10',
* * * * * * *'foo2bar3',
* * * * * * *'foo10bar2',
* * * * * * *'fo',
* * * * * * *'bar1000',
* * * * * * *'777']
* * lines.sort(key=key_func)
* * print '\n'.join(lines)
Perfect!
Thank you.
Oct 9 '08 #5

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

Similar topics

4
by: dont bother | last post by:
This is really driving me crazy. I have a dictionary feature_vectors{}. I try to sort its keys using #apply sorting on feature_vectors sorted_feature_vector=feature_vectors.keys()...
0
by: C. Barnes | last post by:
Summary: Sorts strings in a way that seems natural to humans. If the strings contain integers, then the integers are ordered numerically. For example, sorts into the order . Code:
1
by: Connelly Barnes | last post by:
Summary: Sorts strings in a way that seems natural to humans. If the strings contain integers, then the integers are ordered numerically. For example, sorts into the order . Code: ...
22
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)...
18
by: Scott | last post by:
I have a collection where the items in the collection are dates. I want to iterate over the collection and build a value list string for the rowsource of a listbox. The dates in the collection are...
3
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
6
by: ReMEn | last post by:
My question is this: How can I apply some kind of a function to a listview that allows it to sort by both string and numbers whenever a header is clicked? For example: -------------------...
16
by: Kittyhawk | last post by:
I would like to sort an Arraylist of objects on multiple properties. For instance, I have a Sort Index property and an ID property (both integers). So, the results of my sort would look like this:...
8
by: DierkErdmann | last post by:
Hi ! I know that this topic has been discussed in the past, but I could not find a working solution for my problem: sorting (lists of) strings containing special characters like "ä", "ü",......
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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...

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.