Sorting strings.

Gaute B Strokkenes gs234 at cam.ac.uk
Thu Feb 8 00:28:16 EST 2001


Hello!

One of my current pet projects is a python script that parses and
sorts a library catalogue.  The catalogue is stored as a plain text
file, and all that needs doing is to read in the author, title,
etc. of each book, with some cleverness to detect multi-line entries,
comments etc.

After all this is done, it is time to sort the entries in appropriate
order before we print them out again.  Currently I do this:

def entry_cmp(a, b):
    if a.author < b.author:
        return -1
    if a.author > b.author:
        return 1
    if a.title < b.title:
        return -1
    if a.title > b.title:
        return 1
    if a.prefix < b.prefix:
        return -1
    if a.prefix > b.prefix:
        return 1
    if a.code < b.code:
        return -1
    if a.code > b.code:
        return 1
    return 0

...

ll.sort(entry_cmp)

The problem with this approach is that string comparison doesn't quite
have the behaviour that I would like.  For instance, I'd like numbers
to come after letters, and I'd like to be case insensitive.  Is there
a reasonably clean way to do this?

-- 
Big Gaute                               http://www.srcf.ucam.org/~gs234/
I'm having a BIG BANG THEORY!!



More information about the Python-list mailing list