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

  • Home
  • SEARCH
  • 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 307727
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T07:35:06+00:00 2026-05-12T07:35:06+00:00

For the sake of simplicity, let’s say I have a Person class in Python.

  • 0

For the sake of simplicity, let’s say I have a Person class in Python. This class has fields for firstname, lastname, and dob.

class Person:
  def __init__(self, firstname, lastname, dob):
    self.firstname = firstname;
    self.lastname = lastname;
    self.dob = dob;

In some situations I want to sort lists of Persons by lastname followed by firstname followed by dob. In other situations I want to sort first by dob, then by lastname and finally by firstname. And sometimes I just want to sort by firstname.

The naive solution for creating the first comparison function would be something like this:

def comparepeople(person1, person2):
  if cmp(person1.lastname, person2.lastname) == 0:
    if cmp(person1.firstname, person2.firstname) == 0:
      return cmp(person1.dob, person2.dob);
    return cmp(person1.firstname, person2.firstname);
  return cmp(person1.lastname, person2.lastname);

It would seem like there should be an easy way to define comparison functions like these using a meta-programming approach where all I would need to do is provide the field names in order of precedence instead of writing these very verbose, ugly comparison methods. But I’ve only started playing with Python recently and haven’t found anything like what I’m describing.

So the question is, what is the most Pythonic way to write a comparison function for a class with multiple comparable constituent members?

  • 1 1 Answer
  • 2 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-12T07:35:06+00:00Added an answer on May 12, 2026 at 7:35 am

    If you really want a comparison function, you can use

    def comparepeople(p1, p2):
        o1 = p1.lastname, p1.firstname, p1.dob
        o2 = p2.lastname, p2.firstname, p2.dob
        return cmp(o1,o2)
    

    This relies on tuple comparison. If you want to sort a list, you shouldn’t write a comparison function, though, but a key function:

    l.sort(key=lambda p:(p.lastname, p.firstname, p.dob))
    

    This has the advantage that it is a) shorter and b) faster, because each key gets computed only once (rather than tons of tuples being created in the comparison function during sorting).

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

Sidebar

Related Questions

For the sake of simplicity, lets say I have a CustomerViewModel and I want
This is the edited code: I have a two dimensional stack, like #define push(s,ele)
I have run into a problem where a couple of combos that are bound
I have a simple task to display a fruit object based on certain conditions.
I have have searched all over for a tutorial or something that could show
I need to separate ViewModels in my MVC project from my business models (Data
I am developing a site using Asp.net MVC 3 with Razor. In the _Layout.cshtml
Is is possible to resume downloading a partially downloaded file with curl or similar?
Sorry for the vague title. I'll explain what I'm trying to get working. My
Both List A and List B are of type Products List A = new

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.