Trouble sorting a list of objects by attributes

Robocop bthayre at physics.ucsd.edu
Fri Feb 6 15:31:08 EST 2009


Hello again,
I've found myself stumped when trying to organize this list of
objects.  The objects in question are timesheets which i'd like to
sort by four attributes:

class TimeSheet:
  department = string
  engagement = string
  date = datetime.date
  stare_hour = datetime.time

My ultimate goal is to have a list of this timesheet objects which are
first sorted by departments, then within each department block of the
list, have it organized by projects.  Within each project block i
finally want them sorted chronologically by date and time.

To sort the strings i tried:

timesheets.sort(key=operator.attrgetter('string'))

which is not doing anything to my list unfortunately; it leaves it
untouched.

Giving up on that  i tried to sort the dates or times using:

def sorter_time(a,b):
  if a.engagement == engagement:   # I only want sorting done within
each engagement block
    return cmp(a.start, b.start)
  return 0

timesheets.sort(sorter_time)

But this also did nothing to my list, and at this point i'm incredibly
confused.  From what i know of the involved functions, i would think
that these lines of code (while they wouldn't do exactly what i want
yet) would at least do something to my lists.

Any explanations, or suggestions for a different approach would be
greatly appreciated.  My brain might explode if i continue.




More information about the Python-list mailing list