在python中排序多维JSON对象

在python中排序多维JSON对象,python,json,sorting,jquery-ui-sortable,Python,Json,Sorting,Jquery Ui Sortable,我有一个这样的对象,希望在每个维度(简化的json)中按时间(第一行,第二点)对其进行排序: 这个维度可能更深,并且彼此之间有更多的点/线 排序后的输出将如下所示: [{ "type":"line", "children": [ { "type":"line" }, { "type":"point" }, { "type":"po

我有一个这样的对象,希望在每个维度(简化的json)中按时间(第一行,第二点)对其进行排序:

这个维度可能更深,并且彼此之间有更多的点/线

排序后的输出将如下所示:

[{
    "type":"line",
    "children": [
        {
            "type":"line"
        },
        {
            "type":"point"
        },
        {
            "type":"point"
        }
    ]

},
{
    "type":"point"
},
{
    "type":"point"     
}]

谢谢

您需要以递归方式处理此问题:

from operator import itemgetter

def sortLinesPoints(data):
    if isinstance(data, dict):
        if 'children' in data:
            sortLinesPoints(data['children'])
    else:
        for elem in data:
            sortLinesPoints(elem)
        data.sort(key=itemgetter('type'))

…还有?你在哪里遇到麻烦?你试过什么?(你真的是在用JSON对象,或者——我认为这更可能是——Python数组和字典……?)太棒了,它是有效的,但我该如何反过来做,所以我知道点在行之前?谢谢你的回答。@CezarisLT:将
reversed=True
添加到
sort()
方法中。
from operator import itemgetter

def sortLinesPoints(data):
    if isinstance(data, dict):
        if 'children' in data:
            sortLinesPoints(data['children'])
    else:
        for elem in data:
            sortLinesPoints(elem)
        data.sort(key=itemgetter('type'))