Re: Sorting on multiple values, some ascending, some descending

2007-01-04 Thread Christophe
[EMAIL PROTECTED] a écrit : Raymond Hettinger: The simplest way is to take advantage of sort-stability and do successive sorts. For example, to sort by a primary key ascending and a secondary key decending: L.sort(key=lambda r: r.secondary, reverse=True) L.sort(key=lambda r:

Re: Sorting on multiple values, some ascending, some descending

2007-01-04 Thread Neil Cerutti
On 2007-01-03, dwelden [EMAIL PROTECTED] wrote: I have successfully used the sort lambda construct described in http://mail.python.org/pipermail/python-list/2006-April/377443.html. However, how do I take it one step further such that some values can be sorted ascending and others descending?

Re: Sorting on multiple values, some ascending, some descending

2007-01-04 Thread Peter Otten
Neil Cerutti wrote: Another trick is to factor the key application out of the sort. This may be a good idea if when you want to minimize the number of times your key function is called. The idea is to mangle the list temporarily so you can use an unkeyed sort, and then unmangle the sorted

Re: Sorting on multiple values, some ascending, some descending

2007-01-04 Thread Neil Cerutti
On 2007-01-04, Peter Otten [EMAIL PROTECTED] wrote: Neil Cerutti wrote: Another trick is to factor the key application out of the sort. This may be a good idea if when you want to minimize the number of times your key function is called. The idea is to mangle the list temporarily so you can

Sorting on multiple values, some ascending, some descending

2007-01-03 Thread dwelden
I have successfully used the sort lambda construct described in http://mail.python.org/pipermail/python-list/2006-April/377443.html. However, how do I take it one step further such that some values can be sorted ascending and others descending? Easy enough if the sort values are numeric (just

Re: Sorting on multiple values, some ascending, some descending

2007-01-03 Thread Raymond Hettinger
dwelden wrote: I have successfully used the sort lambda construct described in http://mail.python.org/pipermail/python-list/2006-April/377443.html. However, how do I take it one step further such that some values can be sorted ascending and others descending? Easy enough if the sort values

Re: Sorting on multiple values, some ascending, some descending

2007-01-03 Thread Carsten Haese
On Wed, 2007-01-03 at 10:48 -0800, dwelden wrote: I have successfully used the sort lambda construct described in http://mail.python.org/pipermail/python-list/2006-April/377443.html. However, how do I take it one step further such that some values can be sorted ascending and others descending?

Re: Sorting on multiple values, some ascending, some descending

2007-01-03 Thread bearophileHUGS
Raymond Hettinger: The simplest way is to take advantage of sort-stability and do successive sorts. For example, to sort by a primary key ascending and a secondary key decending: L.sort(key=lambda r: r.secondary, reverse=True) L.sort(key=lambda r: r.primary) That's probably the faster

Re: Sorting on multiple values, some ascending, some descending

2007-01-03 Thread Peter Otten
Raymond Hettinger wrote: dwelden wrote: I have successfully used the sort lambda construct described in http://mail.python.org/pipermail/python-list/2006-April/377443.html. However, how do I take it one step further such that some values can be sorted ascending and others descending? Easy

Re: Sorting on multiple values, some ascending, some descending

2007-01-03 Thread George Sakkis
dwelden wrote: I have successfully used the sort lambda construct described in http://mail.python.org/pipermail/python-list/2006-April/377443.html. However, how do I take it one step further such that some values can be sorted ascending and others descending? Easy enough if the sort values

Re: Sorting on multiple values, some ascending, some descending

2007-01-03 Thread dwelden
The simplest way is to take advantage of sort-stability and do successive sorts. For example, to sort by a primary key ascending and a secondary key decending: L.sort(key=lambda r: r.secondary, reverse=True) L.sort(key=lambda r: r.primary) Excellent! That looks just like what I

Re: Sorting on multiple values, some ascending, some descending

2007-01-03 Thread bearophileHUGS
dwelden wrote: L.sort(key=lambda r: r.secondary, reverse=True) L.sort(key=lambda r: r.primary) Excellent! That looks just like what I needed. Note that there is the (probably little used) operator.attrgetter() too, with that you can avoid the possibly slow lambda: