[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

How to sort over dictionaries

Well, that's a list of... somethings. So I'm assuming you mean sort a list of dictionaries?

foo.sort(key = lambda x: time.strptime(x["date"], "%d-%m-%Y %H:%M"))

with , reverse = True in the sort if you want it sorted in reverse

-----Original Message-----
From: Python-list [mailto:python-list-bounces+david.raymond=tomtom.com at python.org] On Behalf Of harish at moonshots.co.in
Sent: Tuesday, August 28, 2018 11:56 PM
To: python-list at python.org
Subject: How to sort over dictionaries

[<Entity('news', 563322629075763) {'headline': 'headline', 'date': '04-08-2018 19:12', 'votes': 100, 'url': 'headline', 'author': 'headline'}>, <Entity('news', 563447259470976) {'votes': 32, 'url': 'https://getbootstrap.com/', 'author': 'Hari', 'date': '15-08-2018 15:15', 'headline': 'latest news'}>, <Entity('news', 563570314470144) {'votes': 22, 'url': 'delete', 'author': 'delete', 'date': '04-08-2018 01:12', 'headline': 'delete'}>, <Entity('news', 5639445604728832) {'votes': 67, 'url': 'https://www.deps.co/blog/google-cloud-platform-good-bad-ugly/', 'author': 'Harish', 'headline': 'Google Cloud Platform ? The Good, Bad, and Ugly', 'date': '16-08-2018 08:15'}>, <Entity('news', 5641906755207168) {'date': '13-08-2018 09:20', 'headline': 'test', 'votes': 47, 'url': 'wwww.google.com', 'author': 'abc'}>, <Entity('news', 564317288144256) {'headline': 'author', 'date': '04-08-2018 16:45', 'votes': 90, 'url': 'author', 'author': 'author'}>, <Entity('news', 5653401597640704) {'url': 'http://www.dailymail.co.uk/tvshowbiz/harry_styles/index.html', 'author': 'Harry', 'headline': 'Reunion', 'date': '17-08-2018 20:40', 'votes': 3}>, <Entity('news', 565955376993472) {'votes': 8, 'url': 'https://www.telegraph.co.uk/news/2017/07/15/us-vogue-apologises-missing-mark-zayn-malik-gigi-hadid-gender/', 'author': 'zayn', 'date': '17-08-2018 15:39', 'headline': 'Memories'}>]

Can someone help me out how to sort this dictionary data based on time,
how to arrange the same dictionary in descending order based on time.