Context:
In Python 3.9 sorting a list of most objects by a second list is easy, even if duplicates are present:
>>> sorted(zip([5, 5, 3, 2, 1], ['z', 'y', 'x', 'w', 'x']))
[(1, 'x'), (2, 'w'), (3, 'x'), (5, 'y'), (5, 'z')]
If this list to be sorted contains dictionaries, sorting generally goes fine:
>>> sorted(zip([3, 2, 1], [{'z':1}, {'y':2}, {'x':3}]))
[(1, {'x': 3}), (2, {'y': 2}), (3, {'z': 1})]
Issue:
However, when the list to be sorted by contains duplicates, the following error occurs:
>>>sorted(zip([5, 5, 3, 2, 1], [{'z':1}, {'y':2}, {'x':3}, {'w': 4}, {'u': 5}]))
*** TypeError: '<' not supported between instances of 'dict' and 'dict'
The issue sees pretty crazy to me: How do the values of the list to be sorted even affect the list to be sorted by?
Alternative solution:
One, not so elegant solution would be to get the dictionary objects from the list by index:
>>> sort_list = [5, 5, 3, 2, 1]
>>> dicts_list = [{'z':1}, {'y':2}, {'x':3}, {'w': 4}, {'u': 5}]
>>> [dicts_list[i] for _, i in sorted(zip(sort_list, range(len(sort_list))))]
[{'u': 5}, {'w': 4}, {'x': 3}, {'z': 1}, {'y': 2}]
Related Questions on StackOverflow:
Many similar questions have been raised on StackOverflow, related to
- the sorting of a list of dictionaries by the value of the dictionary
- the sorting of a list of dictionaries by their ID's
- the sort order when sorting dictionaries
This specific case, especially including duplicates has not been discussed yet.