[Tutor] samples on sort method of sequence object.

Hugo Arts hugo.yoshi at gmail.com
Thu Jan 14 02:01:16 CET 2010

On Thu, Jan 14, 2010 at 1:47 AM, Lie Ryan <lie.1296 at gmail.com> wrote:
> On 01/14/10 06:56, Hugo Arts wrote:
>> On Wed, Jan 13, 2010 at 8:21 PM, Stefan Behnel <stefan_ml at behnel.de> wrote:
>>> Hugo Arts, 13.01.2010 15:25:
>>>> Here is my solution for the general case:
>>>> from itertools import groupby
>>>> def alphanum_key(string):
>>>>    t = []
>>>>    for isdigit, group in groupby(string, str.isdigit):
>>>>        group = ''.join(group)
>>>>        t.append(int(group) if isdigit else group)
>>>>    return t
>>> Note that this won't work in Py3, where integers and strings are not
>>> ordered, i.e. not comparable w.r.t the < and > operators.
>> True. You can accommodate by writing a ComparableInt class extending
>> int, and implement __lt__ and __gt__.
>> It's not exactly succinct, but it works.
> Not necessary, you can just pass the cmp= parameter to sort that checks
> the chunk's type.

might be interesting to see which performs better. cmp is called more
often, but replacing C versions of __lt__ and __gt__ with python
versions slows things down also, I would presume.


More information about the Tutor mailing list