Sorting of List in python based on an Dictionary inside a Dictionary

I have read most of the entry on this question but nothing matched my need. I tried to improvise still it is not happening(eventhough no error, but not the result what i expected)

This is my JSON string:

{
    "Newcaselist": [
        {
            "RegisteredDate": "2015-07-10 19:05:11",
            "Consultation": {
                "Status": "CALL COMPLETED",
                "CALLID": 862,
                "REC_URL": "41dd0094-9a11-49fb-8eb9-f15ccb41ea77_0_1",
                "Time": "2",
                "Date": "2015-07-10 19:05:11",
                "Type": "CONSULTATION"
            },
            "ID": 4388
        },
        {
            "RegisteredDate": "2015-07-10 19:07:42",
            "Consultation": {
                "Status": "CALL COMPLETED",
                "CALLID": 863,
                "REC_URL": "c2e650a2-6ff0-4adc-9891-4a3a6f49007d_0_1",
                "Time": "2",
                "Date": "2015-07-10 19:07:42",
                "Type": "CONSULTATION"
            },
            "ID": 4389
        },
        {
            "RegisteredDate": "2015-07-10 19:21:41",
            "Consultation": {
                "Status": "CALL COMPLETED",
                "CALLID": 864,
                "REC_URL": "4ac72214-f1d9-4eed-9957-8d0ec2e78278_0_1",
                "Time": "1",
                "Date": "2015-07-10 19:21:41",
                "Type": "CONSULTATION"
            },
            "ID": 4390
        },
        {
            "RegisteredDate": "2015-07-10 19:22:52",
            "Consultation": {
                "Status": "DOCTOR DISCONNECTED",
                "CALLID": 865,
                "REC_URL": "None",
                "Time": "1",
                "Date": "2015-07-10 19:22:52",
                "Type": "CONSULTATION"
            },
            "ID": 4391
        },
        {
            "RegisteredDate": "2015-07-12 12:26:16",
            "Consultation": {
                "Status": "CALL COMPLETED",
                "CALLID": 883,
                "REC_URL": "4c55acf1-f3cd-4014-80a2-142886ea671d_0_1",
                "Time": "1",
                "Date": "2015-07-12 12:28:42",
                "Type": "FOLLOW_UP"
            },
            "ID": 4408
        },
        {
            "RegisteredDate": "2015-07-12 12:54:42",
            "Consultation": {
                "Status": "CALL COMPLETED",
                "CALLID": 892,
                "REC_URL": "0bb769ca-fe74-48de-9324-6315a21e5b8e_0_1",
                "Time": "3",
                "Date": "2015-07-12 13:15:21",
                "Type": "FOLLOW_UP"
            },
            "ID": 4410
        },
        {
            "RegisteredDate": "2015-07-12 14:48:58",
            "Consultation": {
                "Status": "CALL COMPLETED",
                "CALLID": 895,
                "REC_URL": "fdb14e8f-2176-4547-83ae-3e1757e78f50_0_1",
                "Time": "1",
                "Date": "2015-07-12 14:56:58",
                "Type": "FOLLOW_UP"
            },
            "ID": 4413
        },
        {
            "RegisteredDate": "2015-07-15 14:30:49",
            "Consultation": {
                "Status": "CALL COMPLETED",
                "CALLID": 920,
                "REC_URL": "f90593a6-9fcc-432b-b9f0-0e2a7cf33cfe_0_1",
                "Time": "1",
                "Date": "2015-07-15 14:30:49",
                "Type": "CONSULTATION"
            },
            "ID": 4437
        },
        {
            "RegisteredDate": "2015-07-16 20:21:43",
            "Consultation": {
                "Status": "CALL COMPLETED",
                "CALLID": 956,
                "REC_URL": "cd8dbe12-5155-450c-89ff-b043a77f65f6_0_1",
                "Time": "1",
                "Date": "2015-07-16 20:21:43",
                "Type": "CONSULTATION"
            },
            "ID": 4468
        }
    ],
    "Success": false,
    "StatusCode": 600,
    "Message": "Record retrived successfully"
}

I want to sort it order of "Date" inside the Consultation. There are other Objects list Doctor and patient inside the list. For readability I have omited that for to post.

I have tried

sorted(JArray, key=itemgetter('Consultation'))

But it is not sorting.

Answers


First of all, If JArray is the complete json, then you are doing it wrongly , you need to sort the list at JArray["Newcaselist"] , and it would be better to do inplace sorting than use sorted() function.

Secondly, for date you should convert the dates to datetime objects using datetime.datetime.strptime() , example -

import datetime
JArray["Newcaselist"].sort(key = lambda x: datetime.datetime.strptime(x["Consultation"]["Date"],'%Y-%m-%d %H:%M:%S'))

Need Your Help

Primefaces poll stops working when the server sends an empty response

jsf jsf-2 primefaces

I use the poll component from Primefaces 4.0 to make a request to the server every 10 minutes. Everything works just fine most of the time, but every now and then, I don't know why, the server retu...