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.pa...@gmail.com> wrote:

>
>
> > Le 7 avr. 2018 à 21:59, Rob Ede <robjt...@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@mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss
>
_______________________________________________
es-discuss mailing list
es-discuss@mozilla.org
https://mail.mozilla.org/listinfo/es-discuss

Reply via email to