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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T08:33:49+00:00 2026-06-18T08:33:49+00:00

I need to sort this dict with two sequential criteria: rank descending, fruit ascending

  • 0

I need to sort this dict with two sequential criteria: “rank” descending, “fruit” ascending (in case of same ranking result).

The dict is structured in this way

'object':['fruit',rank]

For example I’ve:

myDict = {'item2': ['bananas', 3], 'item3': ['cocumbers', 11], 'item1': ['pinapples', 3], 'item4': ['pears', 3]}

My aim is to obtain this:

{'item3': ['cocumbers', 11], 'item2': ['bananas', 3],'item4': ['pears', 3], 'item1': ['pinapples', 3]}

with

sorted(myDict.items(), key=lambda (k, v): v[1], reverse=True)

I obtain only the correct sorting for rank but with no sorting order for the objects with the same ranking:

[('item3', ['cocumbers', 11]), ('item2', ['bananas', 3]), ('item1', ['pinapples', 3]), ('item4', ['pears', 3])]

How solve this in python 2.7?

Thank’s

  • 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-18T08:33:50+00:00Added an answer on June 18, 2026 at 8:33 am

    sorted(myDict.items(), key=lambda (k, v): (-v[1],v[0]))

    should do the trick. Since rank is an integer, we can easily make it sort descending by making the key function just return the negative value rather than needing the reverse=True keywords to sorted. In the case of a tie, we fall back on the natural ordering of the strings.

    This works because sequences in python sort lexicographically (tuples are a sequence type) — Python compares elements from each sequence in order until one of the elements is non-equal which gives the ordering of your pair of sequences.

    In the case where you can’t easily replace reverse=True with a key function, you’ll need to sort twice:

    l1 = sorted(myDict.items(),key=lambda (k,v):v[0])
    l1.sort(key=lambda (k,v):v[1],reverse=True)
    

    This relies on the documented fact that a python sort must be stable. In other words, the relative order of elements that are equal is the same before and after sorting.

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

Sidebar

Related Questions

I need to sort this array by the subsubkey description ascending. I tried a
I have this array and I need to sort it, first by key_1 and
I need to sort an array that can look like this: $array[4][0] = array('id'
I need to do what to me is an advanced sort. I have this
I need to sort this array by distance Array ( [0] => Array (
Can someone help me i need to sort this array: $report_fields['client_id'] = $row['client_id']; $report_fields['name']
I have the following array. I need to sort this array by the nested
Its easy to sort a dict to list by values, but I need to
For anyone who might have a question like this, you probably need Collections.sort, not
I have the following xml and I need to sort this xml using XSLT

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.