参数data数据格式data = [{‘aa‘:123,‘bb‘:456,‘cc‘:789},{‘aa‘:321,‘bb‘:444,‘cc‘:555},{‘aa‘:888,‘bb‘:259,‘cc‘:1000}]
key为要排序的键
def
bubble_sort(data,key):
length
=
len
(data)
for
i
in
range
(
len
(data)
-
1
):
for
j
in
range
(
len
(data)
-
1
):
if
(data[j][key] < data[j
+
1
][key]):
tmp
=
data[j]
data[j]
=
data[j
+
1
]
data[j
+
1
]
=
tmp
return data
bubble_sort(data, ‘aa‘)
返回结果:[{‘aa‘: 888, ‘cc‘: 1000, ‘bb‘: 259}, {‘aa‘: 321, ‘cc‘: 555, ‘bb‘: 444}, {‘aa‘: 123, ‘cc‘: 789, ‘bb‘: 456}]
时间: 2024-12-27 22:43:58