Giter Club home page Giter Club logo

Comments (15)

Richienb avatar Richienb commented on June 12, 2024 1

It currently throws TypeError: Expected a number.

from num-sort.

sindresorhus avatar sindresorhus commented on June 12, 2024 1

I prefer a breaking change.

from num-sort.

sindresorhus avatar sindresorhus commented on June 12, 2024 1

Correct

from num-sort.

papb avatar papb commented on June 12, 2024

What is the current behavior?

from num-sort.

papb avatar papb commented on June 12, 2024

What is the reasoning for it being considered lowest rather than highest?

from num-sort.

Richienb avatar Richienb commented on June 12, 2024

NaN means not a number which means nothing which means 0.

from num-sort.

papb avatar papb commented on June 12, 2024

Well but 0 is not the lowest possible number 😅

from num-sort.

Richienb avatar Richienb commented on June 12, 2024

0 is the smallest natural number and is usually a fallback for NaN so it's obvious for it to be the smallest number.

from num-sort.

papb avatar papb commented on June 12, 2024

What do you think should be the expected result for [1, -1, NaN].sort(numberSort.ascending)?

from num-sort.

Richienb avatar Richienb commented on June 12, 2024

[NaN, -1, 1]

from num-sort.

papb avatar papb commented on June 12, 2024

So you're treating NaN as -Infinity, not 0.

My opinion is that this is arbitrary and often not intended. How about, instead, adding an option?

const numberSort = require('num-sort');

console.log([1, NaN, -4, 3].sort(numberSort.ascending({ treatNanAs: -Infinity })));
//=> [NaN, -4, 1, 3]

from num-sort.

sindresorhus avatar sindresorhus commented on June 12, 2024

I agree, NaN should be last, after -Infinity.

from num-sort.

sindresorhus avatar sindresorhus commented on June 12, 2024

So you're treating NaN as -Infinity, not 0.

Infinity is technically a number. NaN is not, hence it should be first/last.

from num-sort.

papb avatar papb commented on June 12, 2024

@sindresorhus Do you prefer a breaking change (simply change the existing behavior) by arbitrarily choosing between first/last, or do you think an option such as I suggested is possible?

from num-sort.

papb avatar papb commented on June 12, 2024

@sindresorhus One last confirmation:

[1, -1, -Infinity, NaN].sort(numberSort.ascending)
//=> [NaN, -Infinity, -1, 1]

[1, -1, -Infinity, NaN].sort(numberSort.descending)
//=> [1, -1, -Infinity, NaN]

Is this correct? If yes I'll submit a PR

from num-sort.

Related Issues (1)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.