Proposal: Static sort method on Array
C. Scott Ananian
ecmascript at cscott.net
Sat Apr 7 21:12:53 UTC 2018
To be super explicit: given an array 'a', then: `Array.from(a).sort()` will
return you a sorted clone. That works even if `a` has an iterator or is an
array-like object. That's just seven characters longer than
`Array.sort(a)`, which is what you seem to be proposing.
To be sure, I don't think the wider world (ie, a naive Google search) knows
the full wonders of `Array.from` yet. I particularly like using it on
strings...
--scott
On Sat, Apr 7, 2018 at 5:06 PM, Claude Pache <claude.pache at gmail.com> wrote:
>
>
> > Le 7 avr. 2018 à 21:59, Rob Ede <robjtede at icloud.com> a écrit :
> >
> > I don't like the fact the only way to sort is in-place with Array#sort
> and I can't be the first to feel this way or wonder why there isn't a
> built-in solution.
> >
> > Obviously, searching "javascript array.sort" doesn't produce any helpful
> results to see if someone has suggested this before since all the results
> relate to Array#sort.
> >
> > I'm considering creating a proposal to add an Array.sort() method that
> takes an array and returns a new array. It seems like such a simple
> addition that would remove the need to import lodash, write a helper
> function or use a slightly less clear and possibly slower technique like
> `[...arr].sort()`.
>
> `Array.from` (or indeed `[...arr]`) is a generic built-in way to create a
> new array, and is fine to use in every occasion where you need a copy.
> (Before ES6, there was already `arr.slice(0)`.) I don’t think it is
> appropriate to have an *additional* way to copy arrays for this specific
> case (it would be one more thing to learn).
>
> —Claude
> _______________________________________________
> es-discuss mailing list
> es-discuss at mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20180407/9c0bab81/attachment.html>
More information about the es-discuss
mailing list