I have a priority queue of class objects. In my class definition I have:
def __lt__(self, other):
return self.fn < other.fn
In the event where there are multiple objects that have the same fn value, how do I incorporate a ‘tie breaker’? In other words, something like this:
def __lt__(self, other):
return self.fn < other.fn and self.gn < other.gn
>Solution :
You can add an explicit check for equality between the two fn fields, only checking gn if they’re equal:
def __lt__(self, other):
if self.fn != other.fn:
return self.fn < other.fn
return self.gn < other.gn