You are viewing a single comment's thread. Return to all comments →
In Python 3, I think the template code is incorrect. We can solve it by writing this
def comparator(a, b): if a.score < b.score: return 1 if a.score > b.score: return -1 if a.name < b.name: return -1 if a.name > b.name: return 1 return 0
However if the line
data = sorted(data, key=cmp_to_key(Player.comparator))
was changed to
data = sorted(data, key=Player.key)
we could solve it with the better
def key(self): return(-self.score, self.name)
We can use the second approach by dummying out the cmp_to_key function.
def cmp_to_key(key): return key class Player: def __init__(self, name, score): self.name = name self.score = score def comparator(self): return(-self.score, self.name)
Seems like cookies are disabled on this browser, please enable them to open this website
Sorting: Comparator
You are viewing a single comment's thread. Return to all comments →
In Python 3, I think the template code is incorrect. We can solve it by writing this
However if the line
was changed to
we could solve it with the better
We can use the second approach by dummying out the cmp_to_key function.