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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T09:20:03+00:00 2026-05-26T09:20:03+00:00

So I have a working sorting algorithm in Python. (Its exact contents are irrelevant

  • 0

So I have a working sorting algorithm in Python. (Its exact contents are irrelevant to this question.) It uses a list called ‘people’ containing class instances, and the function is hard-coded to sort that list by a specific attribute, ‘wealth’.

def my_sort(seq):
    # sorts by seq[n].wealth
    ...
my_sort(people)

Now, I’d like to generalize the function so I could sort by any attribute.

def my_sort2(seq, key):
    # sorts by seq[n].key
    ...
my_sort2(people, wealth)

But this, of course, throws an error, because it doesn’t know to consider ‘wealth’ as a class attribute. So, how is this possible to do?

  • 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-26T09:20:04+00:00Added an answer on May 26, 2026 at 9:20 am

    You could pass the name of the attribute:

    def my_sort2(seq, keyname):
        sort by getattr(seq[n], keyname)
    
    my_sort2(people, 'wealth')
    

    or a getter functor:

    def my_sort2(seq, keyfunc):
        sort by keyfunc(seq[n])
    
    my_sort2(people, operator.attrgetter('wealth'))
    

    I prefer the latter approach as it is more generic. For example, it easily allows for computed keys.

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

Sidebar

Related Questions

I am working on an external sorting algorithm that uses std::queue and must carefully
I have been working on some legacy C++ code that uses variable length structures
I have this working definition: IDENTIFIER [a-zA-Z][a-zA-Z0-9]* I don't want to keep repeating the
is it possible to disable onclick= while sorting? I Have a working example here
I have a JQGrid populated with data working correctly. The default sorting functionality is
i have a question on a problem i am working on. I have to
I have this linked list containing hashed values of a set images. I am
So I have this checkbox inside a gridview, and it is working properly for
Say, we have some items, and each defines some partial sorting rules, like this:
T-SQL Paging Sorting & Filtering I have been working on a T-SQL stored procedure

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.