Need Help sorting alphabetically.

Remco Gerlich scarblac at pino.selwerd.nl
Wed Dec 20 12:16:19 CET 2000


Greg Jorgensen <gregj at pobox.com> wrote in comp.lang.python:
> "Remco Gerlich" <scarblac at pino.selwerd.nl> wrote in message
> news:slrn93u8qc.5lh.scarblac at pino.selwerd.nl...
> > Chris Arai <chris at araidesign.com> wrote in comp.lang.python:
> 
> > > 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.
> [snip]
> > It seems you want to ignore case and ignore everything outside a-z.
> > So make a function that throws out all of the other characters and
> > turns the rest into a single case, and then compares those:
> >
> > def compare_alphabetically(a, b):
> >    a = filter(lambda x: x.isalpha(), a.upper())
> >    b = filter(lambda x: x.isalpha(), b.upper())
> >    return cmp(a,b)
> 
> The filter is unnecessary; the upper() and lower() methods of string only
> change alphabetic characters. A simpler solution is:
> 
> def compare_alpha(a, b):
>     return cmp(a.lower(), b.lower())

It's unclear what he wanted exactly, but I think he wanted to sort
purely alphabetically, ignoring the numbers and other characters.
For instance, 'Pg95A_100.gif' sorts before 'Pg100_100.gif'. With
my version it compares "PGAGIF" and "PGGIF" and it works.

But it was pretty unclear what he wanted exactly and why, so who knows :)

-- 
Remco Gerlich




More information about the Python-list mailing list