Custom alphabetical sort

Ian Kelly ian.g.kelly at gmail.com
Mon Dec 24 18:29:29 CET 2012


On Dec 24, 2012 9:37 AM, "Pander Musubi" <pander.musubi at gmail.com> wrote:

> > >>> ''.join(sorted(random.sample(cs, 20), key=d.get))
> >
> > '5aAàÀåBCçËÉíÎLÖøquùx'
>
> This doesn't work for words with more than one character:

Try this instead:

def collate(x):
    return list(map(d.get, x))

sorted(data, key=collate)

I would also probably change "d.get" to "d.__getitem__" for a clearer error
message in the case the string contains characters that it doesn't know how
to sort.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20121224/d129e29c/attachment.html>


More information about the Python-list mailing list