Connecting Tech Pros Worldwide Help | Site Map

Clever way of sorting strings containing integers?

Holger
Guest
 
Posts: n/a
#1: Oct 9 '08
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
Holger
Guest
 
Posts: n/a
#2: Oct 9 '08

re: Clever way of sorting strings containing integers?


On 9 Okt., 09:41, Holger <ish...@gmail.comwrote:
Quote:
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]
Marc 'BlackJack' Rintsch
Guest
 
Posts: n/a
#3: Oct 9 '08

re: Clever way of sorting strings containing integers?


On Thu, 09 Oct 2008 00:41:27 -0700, Holger wrote:
Quote:
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
bieffe62@gmail.com
Guest
 
Posts: n/a
#4: Oct 9 '08

re: Clever way of sorting strings containing integers?


On 9 Ott, 09:41, Holger <ish...@gmail.comwrote:
Quote:
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


Holger
Guest
 
Posts: n/a
#5: Oct 9 '08

re: Clever way of sorting strings containing integers?


On 9 Okt., 10:57, Marc 'BlackJack' Rintsch <bj_...@gmx.netwrote:
Quote:
On Thu, 09 Oct 2008 00:41:27 -0700, Holger wrote:
Quote:
I tried to do this elegantly, but did not come up with a good solution
>
Quote:
Sort strings like
foo1bar2
foo10bar10
foo2bar3
foo10bar2
>
Quote:
So that they come out:
foo1bar2
foo2bar3
foo10bar2
foo10bar10
>
Quote:
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.
Closed Thread