I\'m trying to sort a list and I can\'t figure out to reverse the order of a second sorting characteristic.
import math
ps = {1:(1,1),2:(3,2),3:(3,-3),4:(-3,4),
If your key
-function get's longish and/or complicated you can also use a custom class that compares like you want. It's a bit slower but it can be more readable, especially because you can comment the code:
import math
class Angle_Distance(object):
def __init__(self, x, y):
self.x = x
self.y = y
self.angle = math.atan2(self.y, self.x)
self.distance = math.hypot(self.x, self.y)
def __repr__(self):
return '{self.__class__.__name__}(x={self.x}, y={self.y})'.format(self=self)
def __eq__(self, other):
# this method is not strictly necessary, but makes the class more generally useful.
return self.x == other.x and self.y == other.y
def __lt__(self, other):
if self.angle < other.angle:
return True
elif self.angle == other.angle: # break ties
return self.distance > other.distance
else:
return False
And this can be applied on the list:
>>> ps = {1:(1,1),2:(3,2),3:(3,-3),4:(-3,4),5:(-2,-2),6:(3,3),7:(1,-1)}
>>> l = [Angle_Distance(i, j) for i, j in ps.values()]
>>> sorted(l, reverse=True)
[Angle_Distance(x=-3, y=4),
Angle_Distance(x=1, y=1),
Angle_Distance(x=3, y=3),
Angle_Distance(x=3, y=2),
Angle_Distance(x=1, y=-1),
Angle_Distance(x=3, y=-3),
Angle_Distance(x=-2, y=-2)]
But you could also use it as key
-function:
>>> ps = {1:(1,1),2:(3,2),3:(3,-3),4:(-3,4),5:(-2,-2),6:(3,3),7:(1,-1)}
>>> sorted(ps.values(), key=lambda x: Angle_Distance(x[0], x[1]), reverse=True)
[(-3, 4), (1, 1), (3, 3), (3, 2), (1, -1), (3, -3), (-2, -2)]