Trouble sorting a list of objects by attributes

Robocop bthayre at physics.ucsd.edu
Fri Feb 6 23:38:37 CET 2009


On Feb 6, 2:34 pm, Robocop <btha... at physics.ucsd.edu> wrote:
> On Feb 6, 2:20 pm, Robocop <btha... at physics.ucsd.edu> wrote:
>
>
>
> > On Feb 6, 2:17 pm, Robocop <btha... at physics.ucsd.edu> wrote:
>
> > > On Feb 6, 1:03 pm, bearophileH... at lycos.com wrote:
>
> > > > 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
>
> > > UH OH GUYS!
>
> > > line 110, in sorter
> > >     timesheets.sort(key=attrgetter("department", "engagement",
> > > "date","start"))
> > > TypeError: attrgetter expected 1 arguments, got 4
>
> > I think there may have been a misunderstanding.  I was already using
> > attrgetter, my problem is that it doesn't appear to be sorting by the
> > argument i give it.  How does sort work with strings?  How about with
> > datetime.time or datetime.date?
>
> > So far i can get it sorting strictly by the datetime objects, but i
> > need all of this sorting done within the constraints imposed by doing
> > sorts via department and engagements.
>
> > Any ideas?
>
> I'm stuck with python 2.4 right now:(

I've got a python 2.4 fix though!

 timesheets.sort(key= lambda i:(i.department,i.engagement,i.start))



Thanks for the help and time!!!!!



More information about the Python-list mailing list