<div dir="ltr"><div class="gmail_default" style="font-family:arial,helvetica,sans-serif;font-size:small;color:#0b5394">I should also say that you can replace k['date'] in the lambda with k['title'] or k['url'] to sort by other keys (although I don't know off hand the result of k['url']</div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif;font-size:small;color:#0b5394"><br></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Fri, Feb 22, 2019 at 6:44 PM James Dugger <<a href="mailto:james.dugger@gmail.com">james.dugger@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><blockquote style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex" class="gmail_quote"><pre style="color:rgb(0,0,0);font-family:Monaco;font-size:10.5pt"><span style="color:rgb(0,0,128);font-weight:bold">import </span>json<br><span style="color:rgb(0,0,128);font-weight:bold">from </span>datetime <span style="color:rgb(0,0,128);font-weight:bold">import </span>datetime<br><br><span style="color:rgb(0,0,128);font-weight:bold">with </span>open(<span style="color:rgb(0,128,0);font-weight:bold">'testp.json'</span>) <span style="color:rgb(0,0,128);font-weight:bold">as </span>f:<br> data = json.load(f)<br><br><span style="color:rgb(0,0,128);font-weight:bold">for </span>i <span style="color:rgb(0,0,128);font-weight:bold">in </span>data[<span style="color:rgb(0,128,0);font-weight:bold">'pubs'</span>]:<br> pub_date = datetime.strptime(i[<span style="color:rgb(0,128,0);font-weight:bold">'date'</span>], <span style="color:rgb(0,128,0);font-weight:bold">'%Y%m%d'</span>)<br> i[<span style="color:rgb(0,128,0);font-weight:bold">'date'</span>] = pub_date<br><br>pub_list = sorted(data[<span style="color:rgb(0,128,0);font-weight:bold">'pubs'</span>], <span style="color:rgb(102,0,153)">key</span>=<span style="color:rgb(0,0,128);font-weight:bold">lambda </span>k: k[<span style="color:rgb(0,128,0);font-weight:bold">'date'</span>])<br><br><span style="color:rgb(0,0,128);font-weight:bold">print</span>(pub_list)</pre></blockquote><div><span class="gmail_default" style="font-family:arial,helvetica,sans-serif;font-size:small;color:rgb(11,83,148)">open parses the json file into a python dictionary. Currently the format of your json will produce a dictionary with one key 'pubs' and a value formatted as a python list of nested dictionaries.<br><br>The for loop targets the list inside the main dictionary and first converts the date to a python date object.<br><br></span> <span class="gmail_default" style="font-family:arial,helvetica,sans-serif;font-size:small;color:rgb(11,83,148)">Then simply use the sorted method with a lambda expression to sort the order of dictionaries by the newly created date objects.</span></div><div><span class="gmail_default" style="font-family:arial,helvetica,sans-serif;font-size:small;color:rgb(11,83,148)"><br></span></div><div><span style="color:rgb(11,83,148);font-family:arial,helvetica,sans-serif"><span class="gmail_default" style="font-family:arial,helvetica,sans-serif;font-size:small;color:rgb(11,83,148)">Yor initial date values are integers. You will need to either store them in the json object as strings or convert them so that datetime.strptime() can parse them</span><span class="gmail_default" style="font-family:arial,helvetica,sans-serif;font-size:small;color:rgb(11,83,148)">.</span></span></div><div><span style="color:rgb(11,83,148);font-family:arial,helvetica,sans-serif"><span class="gmail_default" style="font-family:arial,helvetica,sans-serif;font-size:small;color:rgb(11,83,148)"><br></span></span></div><div><span style="color:rgb(11,83,148);font-family:arial,helvetica,sans-serif"><span class="gmail_default" style="font-family:arial,helvetica,sans-serif;font-size:small;color:rgb(11,83,148)">I'm sure there are other ways to do this but this was the fastest given the json format.</span></span></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Fri, Feb 22, 2019 at 12:21 PM der.hans <<a href="mailto:PLUGd@lufthans.com" target="_blank">PLUGd@lufthans.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">moin moin,<br>
<br>
I'm trying to sort some data that is currently in json being imported into<br>
a dict.<br>
<br>
It's essentially bibliography information, but without a unique ID.<br>
<br>
I mostly want to sort on publication date, but also want to group by<br>
publication. There could be multiple publications on the same date.<br>
<br>
Currently using a json format like:<br>
<br>
{<br>
"pubs": [<br>
{<br>
"date": 20190222<br>
"title": "a title"<br>
"url": "<a href="https://some.domain/page.html" rel="noreferrer" target="_blank">https://some.domain/page.html</a>"<br>
},<br>
{<br>
"date": 20190221<br>
"title": "another title"<br>
"url": "<a href="https://some.other.domain/page.html" rel="noreferrer" target="_blank">https://some.other.domain/page.html</a>"<br>
}<br>
]<br>
}<br>
<br>
Format can change if that helps.<br>
<br>
I want to be able to display items in order. Mostly chronological, but<br>
also by other details.<br>
<br>
Any suggestions for how to sort them for display?<br>
<br>
A nice, but not required, feature would be sorting them for saving to disk<br>
as well.<br>
<br>
ciao,<br>
<br>
der.hans<br>
-- <br>
# <a href="https://www.LuftHans.com" rel="noreferrer" target="_blank">https://www.LuftHans.com</a> <a href="https://www.PhxLinux.org" rel="noreferrer" target="_blank">https://www.PhxLinux.org</a><br>
# "You want weapons? We're in a library! Books! The best weapons in the<br>
# world! This room's the greatest arsenal we could have - arm yourselves!"<br>
# -- the Doctor: Doctor Who, Tooth and Claw, 2006<br>
---------------------------------------------------<br>
PLUG-discuss mailing list - <a href="mailto:PLUG-discuss@lists.phxlinux.org" target="_blank">PLUG-discuss@lists.phxlinux.org</a><br>
To subscribe, unsubscribe, or to change your mail settings:<br>
<a href="https://lists.phxlinux.org/mailman/listinfo/plug-discuss" rel="noreferrer" target="_blank">https://lists.phxlinux.org/mailman/listinfo/plug-discuss</a></blockquote></div><br clear="all"><div><br></div>-- <br><div dir="ltr" class="gmail-m_-6729680232614274808gmail_signature"><div dir="ltr"><font color="#0b5394">James</font><div><br><span style="color:rgb(255,255,255)"><span style="background-color:rgb(11,83,148)"><b><a href="http://www.linkedin.com/pub/james-h-dugger/15/64b/74a/" target="_blank"><span style="background-color:rgb(255,255,255)"><span></span><span style="color:rgb(11,83,148)">Linkedin<span></span></span></span></a></b></span></span><br></div></div></div>
</blockquote></div><br clear="all"><div><br></div>-- <br><div dir="ltr" class="gmail_signature"><div dir="ltr"><font color="#0b5394">James</font><div><br><span style="color:rgb(255,255,255)"><span style="background-color:rgb(11,83,148)"><b><a href="http://www.linkedin.com/pub/james-h-dugger/15/64b/74a/" target="_blank"><span style="background-color:rgb(255,255,255)"><span></span><span style="color:rgb(11,83,148)">Linkedin<span></span></span></span></a></b></span></span><br></div></div></div>