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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T16:08:09+00:00 2026-06-06T16:08:09+00:00

The move in recent versions of Python to passing a key function to sort()

  • 0

The move in recent versions of Python to passing a key function to sort() from the previous cmp function is making it trickier for me to perform complex sorts on certain objects.

For example, I want to sort a set of objects from newest to oldest, with a set of string tie-breaker fields. So I want the dates in reverse order but the strings in their natural order. With a comparison function I can just reverse the comparison for the date field compared to the string fields. But with a key function I need to find some way to invert/reverse either the dates or the strings.

It’s easy (although ugly) to do with numbers – just subtract them from something – but do I have to find a similar hack for dates (subtract them from another date and compare the timedeltas?) and strings (…I have no idea how I’d reverse their order in a locale-independent way).

I know of the existence of functools.cmp_to_key() but it is described as being “primarily used as a transition tool for programs being converted to Python 3 where comparison functions are no longer supported”. This implies that I should be able to do what I want with the key method – but how?

  • 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-06T16:08:11+00:00Added an answer on June 6, 2026 at 4:08 pm

    The slow-but-elegant way to do this is to create a value wrapper that has reversed ordering:

    from functools import total_ordering
    @total_ordering
    class ReversedOrder:
        def __init__(self, value):
            self.value = value
        def __eq__(self, other):
            return other.value == self.value
        def __lt__(self, other):
            return other.value < self.value
    

    If you don’t have functools.total_ordering, you’d have to implement all 6 comparisons, e.g.:

    import operator
    class ReversedOrder:
        def __init__(self, value):
            self.value = value
    for x in ['__lt__', '__le__', '__eq__', '__ne__', '__ge__', '__gt__']:
        op = getattr(operator, x)
        setattr(ReversedOrder, x, lambda self, other, op=op: op(other.value, self.value))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new to Django and Python. When I move my code from my local
I am trying to move some files in python, but they have spaces in
I'm trying to move to Python 2.7 and since Unicode is a Big Deal
Continuing a previous question I asked here , I now need to move to
I'm looking to move small files (at unknown times and in unknown quantities) from
How do I move my recent commits on master to a new branch, and
I'm trying to create a sort of tree diagram, in which the most recent
I move a UIImageView with touchesBegan , touchesMoved , touchesEnded . It works well,
I am trying to move even numbers in an array to the front and
I am trying to move an object by checking if the mouse is colliding

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.