Spaceship Operator

Claude Pache claude.pache at gmail.com
Wed Jun 28 09:30:06 UTC 2017


> Le 27 juin 2017 à 22:19, Mike Samuel <mikesamuel at gmail.com> a écrit :
> 
> On Tue, Jun 27, 2017 at 4:13 PM, Isiah Meadows <isiahmeadows at gmail.com> wrote:
>> For what it's worth, for most practical purposes, `arr.sort((a, b) => b -
>> a))` works well enough. (The only thing it doesn't work well with are NaNs,
>> but in practice, those almost never occur.)
> 
> Don't numeric comparison operators typically sort -0 before +0?
> 
> ((a,b)=>b-a) is also problematic for an array that contains two or
> more infinite values with the same sign and one or more finite values
> since isNaN(Infinity-Infinity).   That NaN from the comparator can be
> triggered or not based on details of the sorting algo and the precise
> placement on the Infinities.

When the comparison function produces NaN, it is treated the same way as 0; so no it is not a problem for infinities.
More precisely, because `(Infinity - Infinity)` is NaN,  `Infinity` is treated as “equal” to `Infinity`, which is correct.

It is an issue with `NaN`, because `NaN` will be treated as “equal” to any value.

—Claude



More information about the es-discuss mailing list