Message96024
Tom, I think I'm missing your point: all three of the examples you give
seem like perfect candidates for a key-based sort rather than a
comparison-based one. For the first example, couldn't you do something
like:
def direction(pt1, pt2):
"""angle of line segment from point 1 to point 2"""
return atan2(pt2.y - pt1.y, pt2.x - pt1.x)
my_points.sort(key=lambda pt: direction(reference_pt, pt))
? How would having a cmp keyword argument make this any easier or
simpler?
Here's the best example I can think of for which key-based sorting is
problematic: imagine that the Decimal type doesn't exist, and that you
have triples (sign, coefficient_string, exponent) representing
arbitrary-precision base 10 floating-point numbers. It's fairly tricky
to come up with a key function that maps these triples to some existing
ordered type, so that they can be sorted in natural numerical order.
The problem lies in the way that the sort order for the coefficient
string and exponent depends on the value of the sign (one way for
positive numbers, reversed for negative numbers). But it's not a big
deal to define a wrapper for cases like this. |
|
Date |
User |
Action |
Args |
2009-12-06 11:57:31 | mark.dickinson | set | recipients:
+ mark.dickinson, gvanrossum, rhettinger, LeWiemann, tixxit |
2009-12-06 11:57:31 | mark.dickinson | set | messageid: <1260100651.05.0.868730760351.issue1771@psf.upfronthosting.co.za> |
2009-12-06 11:57:29 | mark.dickinson | link | issue1771 messages |
2009-12-06 11:57:28 | mark.dickinson | create | |
|