I have a list whose elements are dictionaries with a value and a type field, i.e.:
my_list = [{'val':5, 'type':0},{'val':6, 'type':2},{'val':2, 'type':1},{'val':9, 'type':0}]
I would like to sort this list in descending order based on the type
field and within each type based on the value
field, and obtain a vector with the corresponding indexes of the sorting along with the sorted vector.
I know how to do it for an individual criteria using a lambda
function, i.e.:
sorted_list = sorted(my_list, key=lambda k: k['type'], reverse=True)
but how do I extend it to multiple criteria?
Desired output:
sorted_list = [{'val':6, 'type':2},{'val':2, 'type':1},{'val':9, 'type':0},{'val':5, 'type':0}]
sorted_idxs = [1, 2, 3, 0]`, such that `[my_list[k] for k in sorted_idxs]==sorted_list