Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8001131
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T16:00:36+00:00 2026-06-04T16:00:36+00:00

Alright. This may be difficult but I have been struggling for quite a bit

  • 0

Alright. This may be difficult but I have been struggling for quite a bit without great improvement, so I’d like to know what you guys think.

Suppose I have the following list of objects:

objects = [
        {'id': '1', 'w': 0.20}, 
        {'id': '1.1', 'w': 0.80}, 
        {'id': '1.2', 'w': 0.20},
        {'id': '1.3', 'w': 0.30},
        {'id': '1.1.1', 'w': 0.60},
        {'id': '1.1.2', 'w': 0.70},
        {'id': '1.1.3', 'w': 0.40},
        {'id': '1.2.1', 'w': 0.30},
    ]

I want to sort this list by ‘id’ (e.g. '1', '1.1', '1.1.1', '1.1.2', '1.1.3', '1.2', '1.2.1', '1.3') but then all the elements with the same parent need to be ordered by ‘w’ (in reverse). What do I mean by ‘same parent’?. Well, ‘1’ is parent of ‘1.1’, ‘1.2’ and ‘1.3’. Likewise, ‘1.1’ is parent of ‘1.1.1’, ‘1.1.2’, ‘1.1.3’ and ‘1.2’ is parent of ‘1.2.1’. To illustrate this better, imagine this is a representation of a thread with nested comments (‘1’ is the original post, ‘1.1’ is its answer, and so on).

For now, I have been able to reach the following form:

[ [ {'w': 0.2, 'id': '1'} ], [ {'w': 0.8, 'id': '1.1'}, {'w': 0.3, 'id': '1.3'}, 
{'w': 0.2, 'id': '1.2'} ], [ {'w': 0.7, 'id': '1.1.2'}, {'w': 0.6, 'id': '1.1.1'},
{'w': 0.4, 'id': '1.1.3'} ], [ {'w': 0.3, 'id': '1.2.1'} ] ]

As you can see, each nested list comprises elements which are children of other element. For example, the second nested list [ {'w': 0.8, 'id': '1.1'}, {'w': 0.3, 'id': '1.3'}, {'w': 0.2, 'id': '1.2'} ] contains all the children of the element [ {'w': 0.2, 'id': '1'} ]. Additionally, each nested list is ordered by ‘w’.

The final result should be something like this (assuming chaining all the inner lists – list(itertools.chain(*b))):

{'id': '1', 'w': 0.20}, {'id': '1.1', 'w': 0.80}, {'id': '1.1.2', 'w': 0.70}, 
{'id': '1.1.1', 'w': 0.60}, {'id': '1.1.3', 'w': 0.40}, {'id': '1.3', 'w': 0.30}, 
{'id': '1.2', 'w': 0.20},   {'id': '1.2.1', 'w': 0.30}

Basically, first goes the parent, then its children (ordered by ‘w’), and the same applies to each element (if it has children, of course – here {'id': '1.3', 'w': 0.30} doesn’t have children so we don’t need to do anything with it).

I have tried a few things (too convoluted to be worthy of explanation). I ended up with quite a few conditionals and an ugly code.

How can I accomplish this sorting?.

Thanks in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T16:00:37+00:00Added an answer on June 4, 2026 at 4:00 pm

    A simple sort will not solve your problem, because it is not possible to compare any two elements and immediately know when one comes before another (the weight of the parents may change the ordering).

    You need to process the list into a tree structure and then extract it in order:

    tree = {}
    
    for d in objects:
        ids = d['id'].split('.')
        w = d['w']
        # walk into the tree, creating nodes as necessary
        subtree = [0,tree]
        for n in ids:
            if n not in subtree[1]:
                subtree[1][n] = [0,{}] # w, list of nodes
            subtree = subtree[1][n] # recurse
        # subtree is now the relevant node, set w
        subtree[0] = w
    
    ## now we have a tree:
    ## >>> pprint.pprint(tree, width=10)
    ## {'1': [0.2,
    ##       {'1': [0.8,
    ##              {'1': [0.6,
    ##                     {}],
    ##               '2': [0.7,
    ##                     {}],
    ##               '3': [0.4,
    ##                     {}]}],
    ##        '2': [0.2,
    ##              {'1': [0.3,
    ##                     {}]}],
    ##        '3': [0.3,
    ##              {}]}]}
    
    # now walk the tree and extract the nodes:
    result = []
    def walk_subtree(subtree, path=[]):
        keyweights = [(subtree[key][0], key) for key in subtree]
        # walk through nodes at this level, outputting.
        for weight, key in sorted(keyweights, reverse=True):
            result.append(('.'.join(path + [key]), weight))
            walk_subtree(subtree[key][1], path=path+[key])
    
    walk_subtree(tree)
    
    ##>>> pprint.pprint(result)
    ##[('1', 0.2),
    ## ('1.1', 0.8),
    ## ('1.1.2', 0.7),
    ## ('1.1.1', 0.6),
    ## ('1.1.3', 0.4),
    ## ('1.3', 0.3),
    ## ('1.2', 0.2),
    ## ('1.2.1', 0.3)]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Alright I know this is more than likely a amateur question but I have
Alright, this one's interesting. I have a solution, but I don't like it. The
Alright. Now this question may come to you weird but i have to solve
Alright, I know I've asked similar questions, but I feel this is hopefully a
This may be a very simple issue, but I've been stuck with this for
Alright - this may be a dumb question, but my desire for perfection is
Alright, this has caused me enough grief. The answer may be dead simple, but
Alright, this may take a moment or two to explain: I'm working on creating
Alright this is my first day with JQuery so have fun with these functions
Alright so this problem has been breaking my brain all day today. The Problem:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.