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

Hemanth H.M hemanth.hm at gmail.com
Fri Jun 6 04:42:56 PDT 2014


My bad! Miss read it. ^That still sorts in ascending order only.


On Fri, Jun 6, 2014 at 5:08 PM, Hemanth H.M <hemanth.hm at gmail.com> wrote:

> 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 <mathiasb at opera.com>
> wrote:
>
>> 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?
>> _______________________________________________
>> es-discuss mailing list
>> es-discuss at mozilla.org
>> 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 *
>



-- 
*'I am what I am because of who we all are'*
h3manth.com <http://www.h3manth.com>
*-- Hemanth HM *
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20140606/c366faa9/attachment.html>


More information about the es-discuss mailing list