Trouble sorting a list of objects by attributes

R

Robocop

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.
 
M

Miki

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.

Python "natural" sort for tuples is doing the right thing for you, for
example:
from operator import attrgetter
from random import randint
class Point:
def __init__(self, x, y):
self.x = x
self.y = y

def __repr__(self):
return "(%s, %s)" % (self.x, self.y)


points = [Point(randint(1, 10), randint(1, 10)) for i in range(10)]
print points
points.sort(key=attrgetter("x", "y"))
print points

Gave me:
[(2, 3), (1, 4), (4, 4), (6, 6), (2, 10), (3, 5), (7, 1), (3, 6), (4,
1), (9, 6)]
[(1, 4), (2, 3), (2, 10), (3, 5), (3, 6), (4, 1), (4, 4), (6, 6), (7,
1), (9, 6)]

Points first sorted by 'x' and then by 'y'.

HTH,
 
B

bearophileHUGS

Robocop:
then within each department block of the list, have it organized by projects.<

I don't know what does it means.
timesheets.sort(key=operator.attrgetter('string'))

Try something like:
timesheets.sort(key=attrgetter("department", "engagement", "date",
"stare_hour"))

My brain might explode if i continue.

Relax.

Bye,
bearophile
 
R

Robocop

You were using the attrgetter, but it looks like you weren't doing it correctly:

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

You don't specify a type, be it string or date or anything: you
specify the /name/ of the attribute to get. It'll handle sorting by
any data type that has an order to it without you having to worry
about it at all. Strings, ints, dates, times.

It should be:

   timesheets.sort(key=operator.attrgetter("department"))

If you want to sort by the value of the "department" attribute, which
happens to be a string.
If you want to sort by the "date" attribute, which happens to be a
datetime.date, you do:
   timesheets.sort(key=operator.attrgetter("date"))

Where date is not a datatype a la datetime.date, but the name of an
attribute on your TimeSheet instances.

--S

Yeah this i totally understand. In my code i was using attrgetter
correctly, in the above answer i used string to represent the datatype
of the attribute, not what i actually put in there. More my laziness
than anything else. The problem boiled down attrgetter taking
multiple arguments in 2.5, but only one in 2.4. Thanks for the input
though.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,059
Latest member
cryptoseoagencies

Latest Threads

Top