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 8869243
In Process

The Archive Base Latest Questions

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

I want to order the items in a dictionary using various comparator functions. Please

  • 0

I want to order the items in a dictionary using various comparator functions.
Please see my example code below. It is the last part using cmpRatio function with sorted() that does not work. I am not sure what I am doing wrong. Thanks in advance for any idea!

mydict = { 'a1': (1,6),
          'a2': (10,2),
          'a3': (5,3),
          'a4': (1,2),
          'a5': (3,9),
          'a6': (9,7) }

# sort by first element of the value tuple: WORKS
print sorted(mydict.iteritems(), key=lambda (k,v): v[0])

# sort by second element of the value tuple: WORKS
print sorted(mydict.iteritems(), key=lambda (k,v): v[1])

# THIS is what I can't get working:
def cmpRatio(x,y):
   sx = float(x[0])/x[1]
   sy = float(y[0])/y[1]
   return sx < sy

# sort by sum of the elements in the value tuple: DOES NOT WORK
print sorted(mydict.iteritems(), key=lambda (k,v): v, cmp=cmpRatio)
  • 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-14T17:26:37+00:00Added an answer on June 14, 2026 at 5:26 pm

    Avoid cmp functions where possible because they are slow. They have to be re-evaluated for each comparison. Using a key makes it so the key only needs to be computed once.

    print sorted(mydict.iteritems(), key=lambda (k,v): float(v[0])/v[1])
    

    Also, you say you want to sort by the sum of the value items, yet you are sorting by the difference. Sum would look like:

    print sorted(mydict.iteritems(), key=lambda (k,v): sum(v))
    

    As mentioned in other answers, for the purpose of really wanting to define a cmp function, you are not returning the proper value (must be -1,0, or 1).

    return cmp(sx,sy)
    

    But also if you are just using a lambda to get the value, you can replace that with itemgetter which should be faster than a python-side function:

    from operator import itemgetter
    
    print sorted(mydict.iteritems(), key=itemgetter(1), cmp=cmpRatio)
    

    If you are trying to store up sort operations, it would be much better to store the key functions:

    key_ops = {
        'sum': lambda (k,v): sum(v),
        'ratio': lambda (k,v): float(v[0])/v[1]),
    }
    
    def print_op(aDict, opName):
        print sorted(aDict.iteritems(), key=key_ops[opName])
    
    ... # some place later in code
    print_op(mydict, 'sum')
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to order a result by fieldx * fieldy , for example: Row
I want to have Dictionary that would be 'Observable' in order to throw events
I want order a selection of items by the rating. There are 2 fields:
I want to retrieve how many items there are in the Dictionary array: But
I created a customized CursorAdapter and want to select a list item, in order
MYSQL I want to filter "orders" according to their items within a form's checkboxes.
I want to order grades that are a string in the following order. K,
I want to order by id asc, date asc, but in the case where
I want to order by Time,but seems no way to do that ? mysql>
I want to order values from database query by date but I want also

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.