Custom sorting (including digraphs)

Mike C. Fletcher mcfletch at rogers.com
Wed Mar 12 19:57:46 EST 2003


def getPrefix( word ):
    # determine sorting prefix as you wish, likely
    # using a lookup in a pre-calculated set of
    # some sort (no pun intended)...
    def lookup( word, table=prefixTable ):
        for index in range(1,len(word)):
            if not table.has_key(word[:index]):
                return word[:index-1]
        return word # whole thing was a key
    ...
    return prefix

def sorter( words ):
    set = [(getPrefix(word),word) for word in words]
    set.sort()
    return [item[1] for item in set]

HTH,
Mike

Peter Clark wrote:

>I'm creating a dictionary creation program (as in lexicons, not
>hashes) and have run into a small problem: I would like to implement
>customizable sorting, which would include the ability to sort multiple
>characters as one. For example:
>
>order = "a, b, c, ch, d, e, (etc)"
>
>would sort "cat, celery, cherry" as "cat, cherry, celery". In other
>words, all occurances of "ch" would be sorted as following "c".
>Naturally, this also needs to be able to take Unicode data into
>account (which I think wouldn't be a problem, but you never know), be
>able to scale upwards to trigraphs ("thl" for example) and quadgraphs
>("shch"), as well as be as speedy as possible, since I expect to be
>using it to  alphabetize several thousand entries.
>
>I've looked over at the Cookbook, and while it has some nice recipes
>for custom sorting, but none take into consideration treating multiple
>characters as a single unit. TIA,
>
>:Peter
>  
>

-- 
_______________________________________
  Mike C. Fletcher
  Designer, VR Plumber, Coder
  http://members.rogers.com/mcfletch/








More information about the Python-list mailing list