-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanyobj_sort.py
39 lines (31 loc) · 949 Bytes
/
anyobj_sort.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
'''
File: anyobj_sort.py
Project: 01-DataSturcture
===========
File Created: Tuesday, 21st July 2020 3:16:28 pm
Author: <<LanLing>> (<<[email protected]>>)
===========
Last Modified: Tuesday, 21st July 2020 3:16:32 pm
Modified By: <<LanLing>> (<<[email protected]>>>)
===========
Description: 使对象支持排序
Copyright <<2020>> - 2020 Your Company, <<XDU>>
'''
from operator import attrgetter
# 要排序的类
class User:
def __init__(self, user_id, name):
self.user_id = user_id
self.name = name
def __repr__(self):
return 'User({})'.format(self.user_id)
users = [User(23, 'aa'), User(3, 'aa'), User(99, 'bb')]
# 单属性的排序
print(sorted(users, key=attrgetter('user_id')))
# 多属性的排序
print(sorted(users, key=attrgetter('name', 'user_id')))
# 最小值
a = min(users, key=attrgetter('user_id'))
print(a.user_id, a.name)
# 最大值
print(max(users, key=attrgetter('user_id')))