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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T04:30:08+00:00 2026-05-14T04:30:08+00:00

I have a dictionary: a = {‘100′:12,’6′:5,’88’:3,’test’:34, ’67’:7,’1′:64 } I want to sort this

  • 0

I have a dictionary:

a = {'100':12,'6':5,'88':3,'test':34, '67':7,'1':64 }

I want to sort this dictionary with respect to key so it looks like:

a = {'1':64,'6':5,'67':7,'88':3, '100':12,'test':34 }
  • 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-05-14T04:30:09+00:00Added an answer on May 14, 2026 at 4:30 am

    Like everyone else has pointed out, dictionaries have their own ordering and you can’t just sort them like you would a list.

    One thing I would like to add is that, if you just want to go through the elements of a dictionary in sorted order, that’s just:

    for k in sorted(a):
        print k, a[k] # or whatever.
    

    If you’d rather have a list comprehension (per Alex):

    sortedlist = [(k, a[k]) for k in sorted(a)]
    

    I would like to point out that Alex’s use of key=int won’t work with your example because one of your keys is 'test'. If you really want he numbers sorted before the non-numerics, you’ll have to pass in a cmp function:

    def _compare_keys(x, y):
        try:
            x = int(x)
        except ValueError:
            xint = False
        else:
            xint = True
        try:
            y = int(y)
        except ValueError:
            if xint:
                return -1
            return cmp(x.lower(), y.lower())
            # or cmp(x, y) if you want case sensitivity.
        else:
            if xint:
                return cmp(x, y)
            return 1
    
    for k in sorted(a, cmp=_compare_keys):
        print k, a[k] # or whatever.
    

    Or maybe you know enough about your keys to write a function to convert them into a string (or other object) that sorts right:

    # Won't work for integers with more than this many digits, or negative integers.
    MAX_DIGITS = 10
    def _keyify(x):
        try:
            xi = int(x)
        except ValueError:
            return 'S{0}'.format(x)
        else:
            return 'I{0:0{1}}'.format(xi, MAX_DIGITS)
    
    for k in sorted(a, key=_keyify):
        print k, a[k] # or whatever.
    

    This would be much faster than using a cmp function.

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

Sidebar

Related Questions

I have dictionary Dictionary<string, Point> the Key is c1,c3,c2,t1,,t4,t2 I want to sort them
I have dictionary, like Dictionary<string, bool> accValues = new Dictionary<string, bool>() And I want
Does VBA have dictionary structure? Like key<>value array?
I have Dictionary<string,string> keyValList; its data looks like: <{1} , TEXT NUMBER 1> <{2}
I have Dictionary from string key i want to get Value of corresponding key
Suppose I have Dictionary like this: Dictionary<string, string> values = new Dictionary<string, string>() {
I have a dictionary object called obj_1 and it's values are shown like this:
I have a Dictionary of Dictionarys of Array of Objects. Just like this: A
I have a Dictionary where the key and value are both strings. It's possible
I have a dictionary, user_dict, and create a list for each user value, 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.