Clever way of sorting strings containing integers?

H

Holger

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
 
H

Holger

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]
 
M

Marc 'BlackJack' Rintsch

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 = int(result)
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
 
B

bieffe62

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
 
H

Holger

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 = int(result)
    return result

def main():
    lines = ['foo1bar2',
             'foo10bar10',
             'foo2bar3',
             'foo10bar2',
             'fo',
             'bar1000',
             '777']
    lines.sort(key=key_func)
    print '\n'.join(lines)


Perfect!
Thank you.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top