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

  • SEARCH
  • Home
  • 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 8827715
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T07:26:16+00:00 2026-06-14T07:26:16+00:00

I am trying to sort a recursively sort a 2 level nested default dict.

  • 0

I am trying to sort a recursively sort a 2 level nested default dict. I have not been able to figure out how to do this properly. My problem statement is given below:

  1. First level key must be a natural sort.
  2. Second level key must be sorted in a specific order. I have tried to create a list whose indices represent the order of the elements. My code snippet is given below:

    import operator
    import collections
    trade_group_totals = collections.defaultdict(lambda:collections.defaultdict(float))    
    trade_group_totals['foo']['ABC'] = 100
    trade_group_totals['foo']['XYZ'] = 50
    trade_group_totals['bar']['ABC'] = 150
    trade_group_totals['bar']['XYZ'] = 250
    

My sorting index comparison:

trade_groups = ['XYZ', 'ABC']

def TradeGroupSort(trade_group):
   return trade_groups.index(trade_group)   

def SortTotals(totals, sort_function_one, sort_function_two):
  return [
      (k1, v1) for k1, v1 in [(k, sorted(v.iteritems(), key=sort_function_two))
           for k, v in sorted(totals.iteritems(), key=sort_function_one)]]

I am invoking the function as follows:

SortTotals(
    trade_group_totals, operator.itemgetter(0),
    sort_function_two=lambda x: operator.methodcaller('TradeGroupSort', x))

My expected output should be:

[('bar', [('XYZ', 50), ('ABC', 100)]), ('foo', [('XYZ', 250), ('ABC', 150)])]

But the generated output is

[('bar', [('XYZ', 50), ('ABC', 150)]), ('foo', [('ABC', 150), ('XYZ', 250)])]
  • 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-14T07:26:18+00:00Added an answer on June 14, 2026 at 7:26 am

    Unfortunately, none of the solutions worked for me. This is the UGLY sorting that I came up with.

    def TradeGroupSort(trade_group):
      """Sorting the trade group."""
      group, _ = trade_group  # The value is a tuple ('XYZ', 50) for e.g.
      return trade_groups.index(group)
    

    I could not think of a better solution than that. I invoked this function as follows:

    SortTotals(trade_group_totals, operator.itemgetter(0), TradeGroupSort)
    

    This returns the expected result.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Just need help as I have been trying sort this out for ages now.
Been trying to sort this one out for a while. I'd really appreciate any
Been trying to sort this for over a day now, and I am sure
Hello Trying to sort this list by Month and day. Can not get it
Trying to sort this out... I need a query that will combine both tables
I'm trying to figure out the best way to sort an array under multiple
Trying to sort out the correct syntax for this UPDATE : UPDATE `foo` SET
Trying to sort this array of objects according to (1) depth and (2) weight,
I'm trying to sort by the state of an Organization's main address. I have
I am trying to sort a two-dimensional array by a timestamp column Descending. This

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.