Custom alphabetical sort

Joshua Landau joshua.landau.ws at gmail.com
Thu Dec 27 02:13:11 CET 2012


On 25 December 2012 06:18, Dave Angel <d at davea.name> wrote:

> On 12/24/2012 06:19 PM, Pander Musubi wrote:

 <snip>

>  > Thanks very much for this efficient code.
>
> Perhaps you missed Ian Kelly's correction of Thomas Bach's approach:
>
> d = { k: v for v, k in enumerate(cs) }
>
>
> def collate(x):
>     return list(map(d.get, x))
>
> sorted(data, key=collate)
>
> I'd use Ian Kelly's approach.


Well, he was first to it :P


> It's not only more compact,


I take offence* here! The only difference was "list(map(d.get, x))" vs
"[hashindex[s] for s in string]" (11 chars) and my longer naming scheme. If
you really care enough about those to sway your judgement, shame on you! ;)

* Not really

it shouldn't
> give an exception for a character not in the table.


That was a choice, not a bug. I didn't want undefined behaviour, so I
thought I'd leave it to crash on "bad" input than sort in a way that may be
unwanted. Even Ian Kelly gave this as way of coding it.


> At least, not for
> Python 2.x.  I'm not sure about Python 3, since it can give an exception
> comparing None to int.



Please not that this post was done in humour (but with truth) to delay
sleep. No offence to Ian or you intended ;).

Happy After-Christmas!
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20121227/3c92e185/attachment.html>


More information about the Python-list mailing list