1

I have a nested dict which is used as a category tree.

I want to sort the dictionary by weight values in each branch of the tree.

I searched, tried but i couldn't find a solution.

Thank you in advance

Note: I minimized the dict for legibility.

cat={"categories": 
    {
        "1": {
            "child": {
                "10": {
                    "child": {
                        "12": {
                            "child": {}, 
                            "id": 12, 
                            "weight": 374.0
                        }, 
                        "156": {
                            "child": {}, 
                            "id": 156, 
                            "weight": 93.0
                        }, 
                        "279": {
                            "child": {}, 
                            "id": 279, 
                            "weight": 137.0
                        }, 
                        "280": {
                            "child": {}, 
                            "id": 280, 
                            "weight": 340.0
                        }
                    }, 
                    "id": 10, 
                    "weight": 574.0
                }, 
                "13": {
                    "child": {
                        "14": {
                            "child": {}, 
                            "id": 14, 
                            "weight": 3519.0
                        }, 
                        "15": {
                            "child": {}, 
                            "id": 15, 
                            "weight": 4327.0
                        }, 
                        "264": {
                            "child": {}, 
                            "id": 264, 
                            "weight": 108.0
                        }
                    }, 
                    "id": 13, 
                    "weight": 3646.0
                }
            }, 
            "id": 1, 
            "weight": 8695.0
        }, 
        "6": {
            "child": {}, 
            "id": 6, 
            "weight": 24584.0
        }
    }
}

edit :

expected return ( order by weight desc ):

cat={"categories": 
    {
        "6": {
            "child": {}, 
            "id": 6, 
            "weight": 24584.0
        },
        "1": {
            "child": {
                "13": {
                    "child": {
                        "15": {
                            "child": {}, 
                            "id": 15, 
                            "weight": 4327.0
                        }, 
                        "14": {
                            "child": {}, 
                            "id": 14, 
                            "weight": 3519.0
                        }, 
                        "264": {
                            "child": {}, 
                            "id": 264, 
                            "weight": 108.0
                        }
                    }, 
                    "id": 13, 
                    "weight": 3646.0
                },
                "10": {
                    "child": {
                        "12": {
                            "child": {}, 
                            "id": 12, 
                            "weight": 374.0
                        }, 
                        "280": {
                            "child": {}, 
                            "id": 280, 
                            "weight": 340.0
                        },
                        "279": {
                            "child": {}, 
                            "id": 279, 
                            "weight": 137.0
                        }, 
                        "156": {
                            "child": {}, 
                            "id": 156, 
                            "weight": 93.0
                        }
                    }, 
                    "id": 10, 
                    "weight": 574.0
                }
            }, 
            "id": 1, 
            "weight": 8695.0
        }
    }
}

0 Answers0