__cmp__ method

George Sakkis george.sakkis at gmail.com
Thu Jun 15 05:47:35 CEST 2006


Jon Clements wrote:

> This probably isn't exactly what you want, but, unless you wanted to do
> something especially with your own string class, I would just pass a
> function to the sorted algorithm.
>
> eg:
>
> sorted( [a,b,c], cmp=lambda a,b: cmp(len(a),len(b)) )
>
> gives you the below in the right order...

Or even better in 2.4 or later:
sorted([a,b,c], key=len)

George




More information about the Python-list mailing list