python - Sort a list of dictionary provided an order -


i've list

order = [8, 7, 5, 9, 10, 11] 

and list of dictionaries

list_of_dct = [{'value':11}, {'value':8}, {'value':5}, {'value':7}, {'value':10}, {'value':9}] 

i want sort list_of_dct order given in order list, i.e. output should following:

list_of_dct = [{'value':8}, {'value':7}, {'value':5}, {'value':9}, {'value':10}, {'value':11}] 

i know how sort given key, not when order given. how can sort it?

ps: have o(n^2) solution. looking better solution.

use index of order list sort-just try if every dictionary has 1 value , want sorting value-

sorted(list_of_dct,key=lambda x:order.index(x.values()[0])) 

but if have multiple values 1 key change index (i.e [0]) on sort.


Comments