Multiple "cmp"s chained one after another

Volker Grabsch volker_grabsch at v.notjusthosting.com
Sat May 14 18:48:29 EDT 2005


vincent wehren wrote:
> 
>| > If you don't care about the year, why not just "normalize" the year
>| > to all be the same using the replace method of the date instance?
>|
>| That's a very bad idea. In my example, this would work, but in "reality"
>| I don't sort datetime objects, of course! (Is there any real application
>| where you want to do that?)
>|
>| Instead, I'm sorting "Person" objects using a "birthday" attribute.
>| Since I use these Person objects also in other places, they should never
>| be modified without just to be sorted. In general, the number side effects
>| should always be minimized.
> 
> Can you explain where you see a modification to the orginal object 
> happening?
> (or in any of the other solutions proposed for that matter...)

Sorry, my fault. I didn't read carefully enough.  X-)

> Not here I hope:
> 
>| > datesNorm = [obj.replace(year=1900) for obj in (dates)]
>| > datesNorm.sort()

While you don't change the original objects, there's still a problem since
you're sorting the normalized values. However, I want to sort the original
list (i.e. the list of "Person" objects).

But that's not a real problem if one normalizes in a key function:

	def key_birthday(d):
		return d.replace(year=1900)
	...
	dates.sort(key=key_birthday)

..as suggested in other followups of my posting.

>| Nevertheless, I think you idea is very interesting. Is there any "real"
>| application where normalizing just for sorting would be reasonable?
> 
> How about a case-insensitive sort of strings? (uppering being the 
> normalization step)
> Or getting rid of accented / special characters before sorting.
> These sound like fairly straight-forward use cases to me ;)

For your solution these are good examples. But my question was, whether
normalizing first, and just sorting the normalized values (not the original
values) is reasonable.

I.e., when I sort some strings case-insensitive, I don't want my resulting
(sorted) list to contain only lowercase string. But that's what I would
get if I used the algorithm you described above.


Greets,

-- 
Volker Grabsch
---<<(())>>---
\frac{\left|\vartheta_0\times\{\ell,\kappa\in\Re\}\right|}{\sqrt
[G]{-\Gamma(\alpha)\cdot\mathcal{B}^{\left[\oint\!c_\hbar\right]}}}



More information about the Python-list mailing list