I have a list of dictionaries structured like so.
[
{
'id': 1,
'last_message': {
'sent_at': '2015-10-15T17:48:52.515Z',
'...' : '...'
},
'...' : '...',
},
{
'id': 2,
'last_message': {
'sent_at': '2015-10-15T17:45:52.515Z',
'...' : '...'
},
'...' : '...',
},
{
'id': 3,
'last_message': {
'sent_at': '2015-10-15T17:43:52.515Z',
'...' : '...'
},
'...' : '...',
}
]
And want to sort the list by ['last_message']['sent_at']
.
I tried to do an insertion sort like this, but this results in an infinite loop.
ret = []
for conversation in conversations:
if len(ret) > 1:
for conv in ret:
if conversation['last_message']['sent_at'] > conv['last_message']['sent_at']:
ret.insert(ret.index(conv), conversation)
continue
else:
ret.append(conversation)
What can I do to achieve this?