I'm curious if there is a way to optimize in a situation I'm facing currently.
I have a list of strings representing categories to group and order data by:
['first', 'third', 'second']
This corresponds to a list of dicts containing objects of those categories which need to be sorted according to them:
[{'color':'yellow', 'section':'third'},{'color':'red', 'section':'first'}, {'color': 'blue', 'section':'second'}]
The data list should be sorted via the order given in the first set, in this case resulting in:
[{'color':'red', 'section':'first'},{'color':'yellow', 'section':'third'},{'color': 'blue', 'section':'second'}]
My current solution:
sortedList = []
for section in orderList:
for item in dataList:
if item['section'] == section: sortedList.append(item)
Is there a cleaner way I can be sorting this?