How do I sort this list in Python, if my date is in a String?

前端 未结 5 978
一整个雨季
一整个雨季 2020-11-29 08:38
[{\'date\': \'2010-04-01\', \'people\': 1047, \'hits\': 4522}, {\'date\': \'2010-04-03\', \'people\': 617, \'hits\': 2582}, {\'date\': \'2010-04-02\', \'people\': 73         


        
5条回答
  •  野性不改
    2020-11-29 09:15

    Satoru.Logic's solution is clean and simple. But, per Alex's post, you don't need to manipulate the date string to get the sort order right...so lose the .split('-')

    This code will suffice:

    records.sort(key=lambda x:x['date'])
    

提交回复
热议问题