import random
class Student:
def __init__(name, age, score, height):
self.name, self.age, self.score, self.height = name, age, score, height
def __rper__():
return "(姓名:%s 年龄:%d 成绩:%d 身高:%d)" % (self.name,
self.age, self.score, self.height)
names = ["张三", "李四", "王五", "赵六"]
ls = []
for name in names:
s = Student(name, random.randint(13, 17), random.randint(0, 100), random.randint(110, 190))
ls.append(s)
def sort(ll, fuction):
for i in range(len(ll) - 1):
for j in range(0, len(ll) - 1 - i):
if fuction(ll[j], ll[j + 1]):
ll[j], ls[j + 1] = ll[j + 1], ls[j]
def older_than(stu1, stu2):
return stu1.age > stu2.age
def younger_than(stu1, stu2):
return stu1.age < stu2.age
def score_worse(stu1, stu2):
return stu1.score < stu2.score
def shorter_than(stu1, stu2):
return stu1.height < stu2.height
sort(ls, older_than)
print(ls)
sort(ls, younger_than)
print(ls)
sort(ls, score_worse)
print(ls)
sort(ls, shorter_than)
print(ls)