Something like:

```js
Number.compare = (n1, n2) -> (n1 - n2) / Math.abs(n1 - n2) || 0;
```



On Fri, Jun 6, 2014 at 12:40 PM, Mathias Bynens <[email protected]> wrote:

> On 6 Jun 2014, at 01:15, Axel Rauschmayer <[email protected]> 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?
> _______________________________________________
> es-discuss mailing list
> [email protected]
> https://mail.mozilla.org/listinfo/es-discuss
>



-- 
*'I am what I am because of who we all are'*
h3manth.com <http://www.h3manth.com>
*-- Hemanth HM *
_______________________________________________
es-discuss mailing list
[email protected]
https://mail.mozilla.org/listinfo/es-discuss

Reply via email to