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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T01:28:22+00:00 2026-05-25T01:28:22+00:00

sorted([2, float(‘nan’), 1]) returns [2, nan, 1] (At least on Activestate Python 3.1 implementation.)

  • 0

sorted([2, float('nan'), 1]) returns [2, nan, 1]

(At least on Activestate Python 3.1 implementation.)

I understand nan is a weird object, so I wouldn’t be surprised if it shows up in random places in the sort result. But it also messes up the sort for the non-nan numbers in the container, which is really unexpected.

I asked a related question about max, and based on that I understand why sort works like this. But should this be considered a bug?

Documentation just says “Return a new sorted list […]” without specifying any details.

EDIT:
I now agree that this isn’t in violation of the IEEE standard. However, it’s a bug from any common sense viewpoint, I think. Even Microsoft, which isn’t known to admit their mistakes often, has recognized this one as a bug, and fixed it in the latest version: http://connect.microsoft.com/VisualStudio/feedback/details/363379/bug-in-list-double-sort-in-list-which-contains-double-nan.

Anyway, I ended up following @khachik’s answer:

sorted(list_, key = lambda x : float('-inf') if math.isnan(x) else x)

I suspect it results in a performance hit compared to the language doing that by default, but at least it works (barring any bugs that I introduced).

  • 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-25T01:28:23+00:00Added an answer on May 25, 2026 at 1:28 am

    The previous answers are useful, but perhaps not clear regarding the root of the problem.

    In any language, sort applies a given ordering, defined by a comparison function or in some other way, over the domain of the input values. For example, less-than, a.k.a. operator <, could be used throughout if and only if less than defines a suitable ordering over the input values.

    But this is specifically NOT true for floating point values and less-than:
    “NaN is unordered: it is not equal to, greater than, or less than anything, including itself.” (Clear prose from GNU C manual, but applies to all modern IEEE754 based floating point)

    So the possible solutions are:

    1. remove the NaNs first, making the input domain well defined via <
      (or the other sorting function being used)
    2. define a custom comparison function (a.k.a. predicate) that does
      define an ordering for NaN, such as less than any number, or greater
      than any number.

    Either approach can be used, in any language.

    Practically, considering python, I would prefer to remove the NaNs if you either don’t care much about fastest performance or if removing NaNs is a desired behavior in context.

    Otherwise you could use a suitable predicate function via “cmp” in older python versions, or via this and functools.cmp_to_key(). The latter is a bit more awkward, naturally, than removing the NaNs first. And care will be required to avoid worse performance, when defining this predicate function.

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

Sidebar

Related Questions

Having sorted out an issue with clickonce deployment the client has asked whether we
Having sorted the tag_counts hash via the following code: sorted_tags = Contact.tag_counts.sort{ |x,y| x.name.downcase
I have a sorted collection of objects (it can be either SortedList or SortedDictionary,
In a sorted table, it's common to have an up or a down arrow
Given a sorted vector with a number of values, as in the following example:
Input: 1) A huge sorted array of string SA; 2) A prefix string P;
Is a table intrinsically sorted by it's primary key? If I have a table
I have 4 files sorted alphabetically, A, B, C, and D. These files contain
What is Median of Sorted Arrays in O(log n) complexity ?
I've got a sorted array: array = [[4, 13], [1, 12], [3, 8], [2,

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.