# Python排序问题

``````class Person(object):

def __init__(self, name):
"""Create a person"""
self.name = name
try:
lastBlank = name.rindex(' ')
self.lastName = name[lastBlank + 1:]
except:
self.lastName = name
self.birthday = None

def __lt__(self, other):
"""Returns True if self's name is lexicographically
less than other's name, and False otherwise"""
return self.name < other.name

def __str__(self):
"""Returns self's name"""
return self.name

class PriorityPerson(Person): # the __lt__ part of this class does not work and I don't know why?

def __init__(self,name,deficiencia):
super().__init__(name)
self.deficiente = deficiencia

def __lt__(self, other):
if self.deficiente and other.deficiente and self.name< other.name:
return self.name < other.name
elif self.deficiente and other.deficiente and other.name < self.name:
return other.name< self.name
elif self.deficiente and not other.deficiente and self.name < other.name:
return self.name < other.name
elif self.deficiente and not other.deficiente and other.name < self.name:
return self.name< other.name
elif not self.deficiente and other.deficiente and self.name < other.name:
return other.name < self.name
elif not self.deficiente and other.deficiente and other.name < self.name:
return other.name < self.name
elif not self.deficiente and not other.deficiente and self.name < other.name:
return  self.name < other.name
elif not self.deficiente and not other.deficiente and other.name < self.name:
return other.name < self.name
``````

``````p1 = PriorityPerson("John Smith", False)
p2 = PriorityPerson("Sam Mendes", False)
p3 = PriorityPerson("Grandmother Anne", True)
p4 = PriorityPerson("Stephen Hawking", True)
p5 = PriorityPerson("Betty Large", True)

listaPessoas = [p1,p2,p3,p4, p5]
listaPessoas.sort()
for p in listaPessoas:
print(p)
``````

``````Betty Large
Grandmother Anne
Stephen Hawking
John Smith
Sam Mendes
``````

``````Betty Large
Stephen Hawking
Grandmother Anne
Sam Mendes
John Smith
``````

``````def __lt__(self, other):
if self.deficiente != other.deficiente: # yes, you can compare bools!
return self.deficiente
else:
return self.name < other.name
``````

``````class Person(object):
def __init__(self, name):
"""Create a person"""
self.name = name
try:
lastBlank = name.rindex(' ')
self.lastName = name[lastBlank + 1:]
except:
self.lastName = name
self.birthday = None

def __lt__(self, other):
"""Returns True if self's name is lexicographically
less than other's name, and False otherwise"""
return self.name < other.name

def __str__(self):
"""Returns self's name"""
return self.name

class PriorityPerson(Person):
def __init__(self, name, deficiencia):
super().__init__(name)
self.deficiente = deficiencia

def __lt__(self, other):
if self.deficiente and not other.deficiente:
# If self is VIP and other is not, just return True
return True
elif not self.deficiente and other.deficiente:
# If other is VIP and self is not, just return False
return False

# On equal priority, normal compare
return super().__lt__(other)

p1 = PriorityPerson("John Smith", False)
p2 = PriorityPerson("Sam Mendes", False)
p3 = PriorityPerson("Grandmother Anne", True)
p4 = PriorityPerson("Stephen Hawking", True)
p5 = PriorityPerson("Betty Large", True)

listaPessoas = [p1,p2,p3,p4, p5]
listaPessoas.sort()
for p in listaPessoas:
print(p)

``````

``````Betty Large
Grandmother Anne
Stephen Hawking
John Smith
Sam Mendes
``````