Need Help sorting alphabetically.
Chris Arai
chris at araidesign.com
Wed Dec 20 19:38:15 EST 2000
Hello All!
Thanks for all the efforts to read my mind. Especially Alex who provided a
nice methodical approach, and Duncan who actually read my mind the best.
The purpose of all this was to be able to sort a directory and then be able
to use HTMLgen to create SeriesDocs. I modified Duncan's approach slightly
thinking that it might optimize it ever so slightly (not really an issue in
my case.) Here is what I actually ended up using:
import os,string,re
def make_key(astring,pattern=re.compile(r'(\d+)|([a-z]+)')):
# Used [a-z]+ because \D+ matched "A_"
# Put lower() here so I could use [a-z] instead of [a-zA-Z],
# and only call lower() once
vals = pattern.split(string.lower(astring))
res = []
for i in range(1, len(vals), 3):
if vals[i]:
res.append(int(vals[i]))
else:
res.append(vals[i+1])
return res
def alphasort(list):
l1, res = [], []
for v in list: l1.append((make_key(v), v))
l1.sort()
for k, v in l1: res.append(v)
return res
if __name__=='__main__':
# My real goal was to sort a directory "alphabetically"
# (whatever I meant by that ;-))
m=os.listdir('.')
s=alphasort(m)
print "%20s %20s"% ('Sorted','Unsorted')
for i in range(len(s)):
print "%20s %20s"%(s[i],m[i])
----end snip----
ouput:
bash-2.02$ python alphasort.py
Sorted Unsorted
alphasort.py globtest.py~
alphasort.py~ makethumb.py~
#globtest.py~# makethumb.py
globtest.py~ outtest
makethumb.py Pg66_100.GIF
makethumb.py~ Pg71_300.GIF
outtest Pg71_A_300.gif
outtest2 outtest2
Pg1_100.GIF #globtest.py~#
Pg2_100.GIF alphasort.py~
Pg9_100.GIF alphasort.py
Pg17_100.GIF Pg90_100.GIF
Pg20_100.GIF Pg17_100.GIF
Pg66_100.GIF Pg2_100.GIF
Pg71_300.GIF Pg20_100.GIF
Pg71_A_300.gif Pg9_100.GIF
Pg90_100.GIF Pg1_100.GIF
Thanks again,
Chris
Duncan Booth wrote:
> chris at araidesign.com (Chris Arai) wrote in
> <3A3F1A91.A7ED9182 at araidesign.com>:
>
> >Hi,
> >
> >This seems like it should be obvious, but it is evading me!
> >
> >I would like to sort alphabetically (I'm not sure that is the correct
> >term) so that strings of alpha numerics sort alphabetically instead of
> >by ASCII order.
> >
> >eg:
> >>>> m=['Pg95_100.GIF',
> >'Pg9_100.GIF','Pg95A_100.gif','Pg9A_100.gif','Pg10_100.gif']
> >>>> m.sort()
> >>>> m
> >['Pg10_100.gif', 'Pg95A_100.gif', 'Pg95_100.GIF', 'Pg9A_100.gif',
> >'Pg9_100.GIF']
> >
> >
> >I would like the order to be
> >['Pg9_100.GIF', 'Pg9A_100.gif', 'Pg10_100.gif, 'Pg95_100.GIF',
> >Pg95A_100.gif' ]
> >
> >What is the obvious answer??
> >
> Try this:
> ---- begin cut ---
> import re
> from string import lower
>
> def make_key(value, pattern = re.compile('(\d+)|(\D+)')):
> vals = pattern.split(value)
> res = []
> for i in range(1, len(vals), 3):
> if vals[i]:
> res.append(int(vals[i]))
> else:
> res.append(lower(vals[i+1]))
> return res
>
> def cleversort(list):
> l1, res = [], []
> for v in list: l1.append((make_key(v), v))
> l1.sort()
> for k, v in l1: res.append(v)
> return res
>
> m=['Pg95_100.GIF',
> 'Pg9_100.GIF','Pg95A_100.gif','Pg9A_100.gif','Pg10_100.gif']
>
> print cleversort(m)
>
> ----- end cut ----
> The output is:
> ['Pg9_100.GIF', 'Pg9A_100.gif', 'Pg10_100.gif', 'Pg95_100.GIF',
> 'Pg95A_100.gif'] as you requested.
>
> This sorts each sequence of digits as its numeric value, and each
> sequence of non digits as its lowercase equivalent (which I think is what
> you were asking for).
--
Chris Arai
Arai Design
chris at araidesign.com
http://www.araidesign.com
707 431 1299
FAX 431 1599
-------------- next part --------------
A non-text attachment was scrubbed...
Name: chris.vcf
Type: text/x-vcard
Size: 224 bytes
Desc: Card for Chris Arai
URL: <http://mail.python.org/pipermail/python-list/attachments/20001220/b5d42f26/attachment.vcf>
More information about the Python-list
mailing list