Alphabetical sorts

Ron Adam rrr at ronadam.com
Mon Oct 16 14:23:36 EDT 2006


I have several applications where I want to sort lists in alphabetical order. 
Most examples of sorting usually sort on the ord() order of the character set as 
an approximation.  But that is not always what you want.

The solution of converting everything to lowercase or uppercase is closer, but 
it would be nice if capitalized words come before lowercase words of the same 
spellings.  And I suspect ord() order may not be correct for some character sets.

So I'm wandering what others have done and weather there is something in the 
standard library I haven't found for doing this.

Below is my current way of doing it, but I think it can probably be improved 
quite a bit.

This partial solution also allows ignoring leading characters such as spaces, 
tabs, and underscores by specifying what not to ignore.  So '__ABC__' will be 
next to 'ABC'.  But this aspect isn't my main concern.

Maybe some sort of alphabetical order string could be easily referenced for 
various alphabets instead of having to create them manually?

Also it would be nice if strings with multiple words were ordered correctly.


Cheers,
   _Ron



def stripto(s, goodchrs):
     """ Removes leading and trailing characters from string s
         which are not in the string goodchrs.
     """
     badchrs = set(s)
     for c in goodchrs:
         if c in badchrs:
             badchrs.remove(c)
     badchrs = ''.join(badchrs)
     return s.strip(badchrs)


def alpha_sorted(seq):
     """ Sort a list of strings in 123AaBbCc... order.
     """
     order = ( '0123456789AaBbCcDdEeFfGgHhIiJjKkLlMmNn'
               'OoPpQqRrSsTtUuVvWwXxYyZz' )
     def chr_index(value, sortorder):
         """ Make a sortable numeric list
         """
         result = []
         for c in stripto(value, order):
             cindex = sortorder.find(c)
             if cindex == -1:
                 cindex = len(sortorder)+ord(c)
             result.append(cindex)
         return result

     deco = [(chr_index(a, order), a) for a in seq]
     deco.sort()
     return list(x[1] for x in deco)



More information about the Python-list mailing list