python中基于字典内部字典的列表排序

2024-06-17 15:05:35 发布

您现在位置:Python中文网/ 问答频道 /正文

我已经读了这个问题的大部分条目,但没有一个符合我的需要。我试着即兴发挥,但它没有发生(即使没有错误,但结果不是我所期望的)

这是我的JSON字符串:

{
    "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"
}

我想在咨询中按“日期”排序。列表中还有医生和病人列表中的其他对象。为了便于阅读,我把它省略了。你知道吗

我试过了

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

但这不是分类。你知道吗


Tags: idurldatetimetypestatuscallup
1条回答
网友
1楼 · 发布于 2024-06-17 15:05:35

首先,如果JArray是完整的json,那么您做得不对,您需要在JArray["Newcaselist"]处对列表进行排序,并且进行就地排序比使用sorted()函数更好。你知道吗

其次,对于date,应该使用datetime.datetime.strptime()将日期转换为datetime对象,例如-

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

相关问题 更多 >