You could also achieve that using the __cmp__ magic method[1]: <code>
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)" % (repr(self.name), self.number) b = [] b.append( a('Smith', 1) ) b.append( a('Dave', 456) ) b.append( a('Guran', 9432) ) b.append( a('Asdf', 12) ) b.sort() print b # [a('Asdf', 12), a('Dave', 456), a('Guran', 9432), a('Smith', 1)] </code> The __cmp__ method does the magic, it's used when you call b.sort(). I added __repr__ just to see legible output... [1] http://docs.python.org/ref/customization.html []'s Rodolfo On Sep 21, 5:54 pm, Nianbig <[EMAIL PROTECTED]> wrote: > 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 = [] > > >>>> b.append( a('Smith', 1) ) > > >>>> b.append( a('Dave', 456) ) > > >>>> b.append( a('Guran', 9432) ) > > >>>> b.append( a('Asdf', 12) ) > > > > How do I sort this on their names e.g. ascending? I have tried > > > b.sort() and so on in all sorts of ways but I can´t figure this one > > > out.. > > > this might get you going: > > > def mysortkey(x): > > return x.name > > > b.sort(key=mysortkey) > > > </F> --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~----------~----~----~----~------~----~------~--~---