Re: __cmp__ method

2006-06-15 Thread bruno at modulix
JH wrote: > Hi > > Can anyone explain to me why the following codes do not work? I want to > try out using __cmp__ method to change the sorting order. I subclass > the str and override the __cmp__ method so the strings can be sorted by > the lengh. I expect the shortest string should be in the fro

Re: __cmp__ method

2006-06-14 Thread George Sakkis
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

Re: __cmp__ method

2006-06-14 Thread Jon Clements
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... Never tried doing wh

Re: __cmp__ method

2006-06-14 Thread marek . rocki
Python documentation says: >__cmp__( self, other) > Called by comparison operations if rich comparison (see above) is not defined. So it seems you have to redefine rich comparisons __lt__, __gt__, __eq__ etc as well. If all you need is change sorting order, why not use appropriate parameters of so