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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T18:15:33+00:00 2026-05-24T18:15:33+00:00

Sorting the content of a dictonary by the value has been throughly described already,

  • 0

Sorting the content of a dictonary by the value has been throughly described already, so it can be acheived by something like this:

d={'d':1,'b':2,'c':2,'a':3}
sorted_res_1= sorted(d.items(), key=lambda x: x[1]) 
# or
from operator import itemgetter 
sorted_res_2 = sorted(d.items(), key=itemgetter(1)) 

My question is, what would be the best way to acheive the following output:

[(‘d’, 1), (‘b’, 2), (‘c’, 2), (‘a’, 3)] instead of [(‘d’, 1), (‘c’, 2), (‘b’, 2), (‘a’, 3)]

so that the tuples are sorted by value and then by the key, if the value was equal.

Secondly – would such be possible for reversed:
[(‘a’, 3), (‘b’, 2), (‘c’, 2), (‘d’, 1)] instead of [(‘a’, 3), (‘c’, 2), (‘b’, 2), (‘d’, 1)]?

  • 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-24T18:15:34+00:00Added an answer on May 24, 2026 at 6:15 pm

    The sorted key parameter can return a tuple. In that case, the first item in the tuple is used to sort the items, and the second is used to break ties, and the third for those still tied, and so on…

    In [1]: import operator
    
    In [2]: d={'d':1,'b':2,'c':2,'a':3}
    
    In [3]: sorted(d.items(),key=operator.itemgetter(1,0))
    Out[3]: [('d', 1), ('b', 2), ('c', 2), ('a', 3)]
    

    operator.itemgetter(1,0) returns a tuple formed from the second, and then the first item. That is, if f=operator.itemgetter(1,0) then f(x) returns (x[1],x[0]).

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

Sidebar

Related Questions

I am new to do this sorting technique in jsp page of dynamic content.
I'm sorting a NSMutableArray that has NSString that contain numbers. This is the code
This is interesting, sorting links by content , but how would you go about
I just wondering if one plugin could handle all of this. Content Filtering, Sorting
can't get sorting working for a django-tables2 table. class MyModel(models.Model): pid = models.AutoField('id',primary_key =
I am using JS sorting to call this method: def sort params[:piece].each_with_index do |id,
I was asking myself if there's a better way of sorting content in Drupal.
I have a problem sorting vectors in 2D vector? I would like to sort
I have a Sitecore content structure where any single item can have a number
I'm attempting to add this .click function to each image I'm sorting with the

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.