-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsort.py
More file actions
27 lines (25 loc) · 949 Bytes
/
sort.py
File metadata and controls
27 lines (25 loc) · 949 Bytes
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
class Sort:
@staticmethod
def sort_nested(data, sort_keys=None):
"""
TODO: This is probably too generic and not the best way, but it works for now
"""
if sort_keys is None:
sort_keys = ["start", "code", "title", "name"]
def get_sort_key(item):
return tuple(item.get(key, "") for key in sort_keys)
if isinstance(data, dict):
return {
key: Sort.sort_nested(value, sort_keys)
for key, value in sorted(data.items())
}
elif isinstance(data, list):
if all(isinstance(item, dict) for item in data):
return sorted(
(Sort.sort_nested(item, sort_keys) for item in data),
key=get_sort_key,
)
else:
return sorted(Sort.sort_nested(item, sort_keys) for item in data)
else:
return data