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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T19:17:16+00:00 2026-06-09T19:17:16+00:00

Assume we have a list of 5 tuple: (a, b , c, d, e)

  • 0

Assume we have a list of 5 tuple:

(a, b , c, d, e)

Let the list be student_tuples.

I wish to sort the list different orders for different fields.

The below mentioned command

sorted(student_tuples, key=itemgetter(2,4,0,1))

Will sort the list on ascending order for all the fields.

The below mentioned command

sorted(student_tuples, key=itemgetter(2,4,0,1), reverse=true)

Will sort the list on descending order for all the fields.
What I am looking for is sorting a list on different orders for different fields.
Is there a easy way to do so.

Based on the answers the technique could be used in any language

Thanks,
Gudge

  • 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-09T19:17:17+00:00Added an answer on June 9, 2026 at 7:17 pm

    If the values are numeric, you can do this easily using lambda:

    sorted(student_tuples, key=lambda x: (x[2],x[4],-x[0],x[1]))
                                                    #^ This field will be 
                                                    #  in descending order 
    

    If you can’t easily negate the order inside a lambda function, you need to rely on the stableness of python sorting and sort a few times:

    s = sorted(student_tuples, key=itemgetter(1))
    s.sort(key=itemgetter(0),reversed=True)
    s.sort(key=itemgetter(2,4))
    

    I explain it in more depth in this answer.

    Proof that my answers above accomplish the same thing (with numeric input):

    import random
    def rand_tuple():
        """ Return a random 5-tuple """
        return tuple( random.random() for _ in range(5) )
    
    #100 random 5-tuples
    lst = [ rand_tuple() for _ in range(100) ] 
    
    #sort the list using method 1
    sorted_lst = sorted(lst, key = lambda x: (x[2],x[4],-x[0],x[1])) 
    
    #sort the list in place using method 2
    lst.sort(key = itemgetter(1))  #<- Rightmost tuple element first!!!
    lst.sort(key = itemgetter(0), reversed = True)
    lst.sort(key = itemgetter(2,4))
    
    print (lst == sorted_lst) #True -- Results are the same :-)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So let's assume I have a class named ABC that will have a list
Let's assume we have two collections: List<double> values List<SomePoint> points where SomePoint is a
Let's assume we have a large list of points List<Point> pointList (already stored in
Let's assume I have a list with objects of type Value . Value has
Let's assume I have 2 List<T> List1 and List2 that look like this: List
Let's assume I have a method like this: public static List<T> Get<T>(this SomeObject<T>, Expressions<Func<T,bool>>
Assume that I have a list of employee names from a database (thousands, potentially
I have a list of words (assume they are stored in String[] if you
Assume you have a list item, <li id=foo> which you want to fade from
Assume I have a List<SomeObject> a . Now also assume that I have another

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.