Re: Sorting list of objets in Python/Django

2008-09-21 Thread Steve Holden
You could, but a key function is generally better, and the larger the list the better (in the general case) it is likely to be. The problem is that using __cmp__ requires the sort algorithm to make Python callbacks from a C function for each object-pair comparison. Using the key function, that fun

Re: Sorting list of objets in Python/Django

2008-09-21 Thread Rodolfo
You could also achieve that using the __cmp__ magic method[1]: class a: def __init__(self, name, number): self.name = name self.number = number def __cmp__(self, other): return cmp(self.name, other.name) def __repr__(self): return "a(%s, %s)" % (rep

Re: Sorting list of objets in Python/Django

2008-09-21 Thread Nianbig
Ah, thanks! /Nianbig On 21 Sep, 19:45, Fredrik Lundh <[EMAIL PROTECTED]> wrote: > Nianbig wrote: > > I have a people-list like this: > class a: > > ...     def __init__(self, name, number): > > ...             self.name = name > > ...             self.number = number > > ... > b = []

Re: Sorting list of objets in Python/Django

2008-09-21 Thread Fredrik Lundh
Nianbig wrote: > I have a people-list like this: class a: > ... def __init__(self, name, number): > ... self.name = name > ... self.number = number > ... b = [] b.append( a('Smith', 1) ) b.append( a('Dave', 456) ) b.append( a('Guran', 9432) ) >

Sorting list of objets in Python/Django

2008-09-21 Thread Nianbig
Hi, I have a people-list like this: >>> class a: ... def __init__(self, name, number): ... self.name = name ... self.number = number ... >>> b = [] >>> b.append( a('Smith', 1) ) >>> b.append( a('Dave', 456) ) >>> b.append( a('Guran', 9432) ) >>> b.append( a('Asdf', 12)