Python – how to sort multidimensional list by datetime

Sort the multidimensional List by date.

Example list in a variable stocks:

[[{‘quantity’: (200,), ‘operation_type’: (‘Sell’,), ‘date’: ‘2021-06-05 22:10:29’},
{‘quantity’: (200,), ‘operation_type’: (‘Sell’,), ‘date’: ‘2021-06-05 22:10:29’}]

To sort by date, use the following command:

stocks = sorted(
stocks,
key=lambda x: datetime.strptime(x[[‘date’], ‘% Y-% m-% d% H:% M:% S’), reverse=False
)

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *