# encoding: utf-8
# 版权所有 2024 ©涂聚文有限公司
# 许可信息查看:
# 描述:
# Author : geovindu,Geovin Du 涂聚文.
# IDE : PyCharm 2023.1 python 3.11
# Datetime : 2024/09/28 20:28
# User : geovindu
# Product : PyCharm
# Project : EssentialAlgorithms
# File : example.py
from enum import Enum
class PepoleCollation(Enum):
"""
選擇字段排序
"""
Id=1,
"""
"""
RealName=2,
"""
"""
Tel=3,
"""
"""
Age=4
"""
"""
class Pepole(object):
"""
"""
def __init__(self):
"""
"""
self._id=None
self._realname=None
self._tel=None
self._age=None
@property
def Id(self):
"""
"""
return self._id
@Id.setter
def Id(self,ids):
"""
"""
self._id=ids
@property
def RealName(self):
"""
:return:
"""
return self._realname
@RealName.setter
def RealName(self,realname):
"""
:param realname:
:return:
"""
self._realname=realname
@property
def Tel(self):
"""
:return:
"""
return self._tel
@Tel.setter
def Tel(self,tel):
"""
:param tel:
:return:
"""
self._tel=tel
@property
def Age(self):
"""
:return:
"""
return self._age
@Age.setter
def Age(self,age):
"""
:param age:
:return:
"""
self._age=age
def Sort(table:list[Pepole],check: PepoleCollation,isDesc=True)->list[Pepole]:
"""
object Pepole list
:param table
:param check
:param isDesc
:return
"""
#check=PepoleCollationField.Age
if PepoleCollation.Age==check:
print("age desc",PepoleCollation.Age)
table.sort(key=lambda Pepole: (Pepole.Age,Pepole.Id), reverse=isDesc)
elif PepoleCollation.Id==check:
table.sort(key=lambda Pepole: (Pepole.Id), reverse=isDesc)
elif PepoleCollation.RealName==check:
table.sort(key=lambda Pepole: (Pepole.RealName), reverse=isDesc)
elif PepoleCollation.Tel==check:
table.sort(key=lambda Pepole: (Pepole.Tel), reverse=isDesc)
else:
table.sort(key=lambda Pepole: (Pepole.RealName), reverse=isDesc)
#for info in table:
#print(info.Id,info.RealName,info.Tel,info.Age)
return table
table=[]
p=Pepole()
p.Id='1'
p.RealName="geovindu"
p.Tel="13824350518"
p.Age=28
table.append(p)
p=Pepole()
p.Id='2'
p.RealName="sibo"
p.Tel="13324350518"
p.Age=18
table.append(p)
p=Pepole()
p.Id='3'
p.RealName="zaoti"
p.Tel="13624350518"
p.Age=22
table.append(p)
p=Pepole()
p.Id='4'
p.RealName="jiang"
p.Tel="13524350518"
p.Age=12
table.append(p)
p=Pepole()
p.Id='5'
p.RealName="xiaobiao"
p.Tel="13224350518"
p.Age=20
table.append(p)
# 1
tablesore=sorted(table, key=lambda Pepole: Pepole.Age, reverse=True)
for info in tablesore:
print(info.Id,info.RealName,info.Tel,info.Age)
print('***************')
# 2
Sort(table, PepoleCollation.Age,False)
print('*******2********')
for info in table:
print(info.Id,info.RealName,info.Tel,info.Age)
# 3
Sort(table, PepoleCollation.Age)
print('*******3********')
for info in table:
print(info.Id,info.RealName,info.Tel,info.Age)
# from https://docs.python.org/3/howto/descriptor.html
# from https://docs.python.org/3/library/functions.html
# from https://docs.python.org/3/howto/sorting.html