Idea for ECMAScript 7: Number.compare(a, b)

Mathias Bynens mathiasb at opera.com
Fri Jun 6 00:10:29 PDT 2014


On 6 Jun 2014, at 01:15, Axel Rauschmayer <axel at rauschma.de> wrote:

> It’d be nice to have a built-in way for comparing numbers, e.g. when sorting arrays.
> 
> ```js
> // Compact ECMAScript 6 solution
> // Risk: number overflow
> [1, 5, 3, 12, 2].sort((a,b) => a-b)
> 
> // Proposed new function:
> [1, 5, 3, 12, 2].sort(Number.compare)
> ```

That sorts in ascending order. What if you need to sort in descending order? Would there need to be a built-in function for that too?


More information about the es-discuss mailing list