Pythontr

husonet | Tarih: 05.12.2022

Özel sıralama

Dictler üzerinde özel sıralama

Örnek:
def sorter_specific(data):
sort_specific = ["A", "B", "C", "D"]
return sorted(data, key=lambda x: (sort_specific.index(x['status']), x['start_date']))

data = [{"status": "C", "start_date": "2022-01-06 11:10"}, {"status": "B", "start_date": "2022-01-06 11:09"}]

Output
[{'status': 'B', 'start_date': '2022-01-06 11:09'}, {'status': 'C', 'start_date': '2022-01-06 11:10'}]