[Tutor] Sorting

Timothy M. Brauch tbrauch@mindless.com
Tue, 30 Jul 2002 19:17:21 -0400


I have a class, Participant.  It has attributes last_name, first_name, and
team, among others.  I also have a class Competition.  Members of
Participant are inserted into a list in Competition.  Everything is going
great.

Now for my problem...
I would like to be able to sort the list of Participants in Competition in
specific ways.  I am pretty sure it can be done with sort(cmpfunc), but I
can't figure it out and documention on the cmpfunc for sort seems to be
lacking, or hard to find.  Here is an example session of what I would like
to have.

##Participant(first_name, last_name, team)
##This part works
>>p0 = Participant('Tim', 'Brauch', 'Foo')
>>p1 = Participant('Bob', 'Smith', 'Foo')
>>p2 = Participant('John', 'Doe', 'Bar')
>>p3 = Participant('Jane', 'Doe', 'Bar')
>>C = Competition()
>>C.add_p(p0)
>>C.add_p(p1)
>>C.add_p(p2)
>>C.add_p(p3)

##Now the part I can't get to work...
>>C.sort_by_team()
>>C.display()
Doe, Jane Bar
Doe, John Bar
Brauch, Tim Foo
Smith, Bob Foo

>>C.sort_alpha()
>>C.display()
Brauch, Tim Foo
Doe, Jane Bar
Doe, John Bar
Smith, Bob Foo

I tried writing sort_by_team() by scratch using if statements, but somehow
there is an error in my statements and possibly my identing, but at
something like 15 lines to sort a list, I think I am on the wrong track.
So, I would greatly appreciate it if someone could help me with the sort().

 - Tim