CKTIS / Python
object.__lt__
The strict less-than method supplies the comparison used by sorting.
Version: Python 3.14; repository image, production runtime not attested
Syntax
left < rightThe strict less-than method supplies the comparison used by sorting.
object.__lt__
Job compares priority, so a tie is false in both directions. The reflected counterpart is __gt__, with priority for a strict right-hand subclass. If both sides decline with NotImplemented the operator raises TypeError.
Stable sorted places b first and keeps tied a before c.
class Base:
def __lt__(self,other): return False
class Child(Base):
def __gt__(self,other): return True
assert Base()<Child()
class Unknown: pass
try: Unknown()<Unknown()
except TypeError: pass
else: raise AssertionError
Examples
class Job:
def __init__(self, name, priority):
self.name, self.priority = name, priority
def __lt__(self, other):
if not isinstance(other, Job): return NotImplemented
return self.priority < other.priority
jobs=[Job("a",2), Job("b",1), Job("c",2)]
print([job.name for job in sorted(jobs)], jobs[0] < jobs[2])
Result
['b', 'a', 'c'] False
Stable sorted places b first and keeps tied a before c.