0

I'm trying to sort a json list from another list. example:

jsonList = [{'id': 'das', 'name': 'something'}, {'id': 'rtn', 'name': 'Something Else'}, {'id': 'ddsn', 'name': 'Something ElseElse'}]
orderList = ['rtn', 'ddsn', 'das']

goodList = someFunction(jsonList, orderList )

I need the output to be the json list sorted by the id:

goodList = [{'id': 'rtn', 'name': 'Something Else'}, {'id': 'ddsn', 'name': 'Something ElseElse'}, {'id': 'das', 'name': 'something'}]
Dug
  • 75
  • 1
  • 4

1 Answers1

1

goodList = sorted(jsonList, key=lambda x: orderList.index(x['id']))

or if you want just sort by id

sorted(jsonList, key=lambda x : x['id'])

DeapSim
  • 26
  • 4