`slice()` is better than `Array.from()` if you already have an array because you can chain it with the other Array.prototype methods.
Good point about not needing it after you've done a map/filter/concat or whatever, since you already have a new array. However I agree with the thrust of a proposal that produces a new array from sort instead of in-place, at least from when `sort` was being introduced. I have made bugs on this presumption with sort(), until I learned it is in-place. However, since sort() exists as it is now, it could be too confusing to have 2 `sort`s in JavaScript. If this is the case, we may have to accept this as a JavaScript language mistake in hindsight that we have to work around using slice(), specific to sort (but not the other Array.prototype methods). But, if it's not too confusing, then I would have no problem with e.g.: `Array.prototype.sortedShallowClone` being introduced to the language. On Sun, 8 Apr 2018 at 03:48 T.J. Crowder <[email protected]> wrote: > On Sat, Apr 7, 2018 at 8:59 PM, Rob Ede <[email protected]> wrote: > > ...I'm considering creating a proposal to add an Array.sort() > > method that takes an array and returns a new array... > > That would be: > > ```js > let newArray = originalArray.slice().sort(); > // or > let newArray = Array.from(originalArray).sort(); > // or > let newArray = [...originalArray].sort(); > ``` > > I don't know that we need a new static for it. Unless the motivation is to > allow insertion sort or other sort algorithms that work best when creating > a new array as a result? But if we assume `Array.prototype.sort` already > uses quicksort or mergesort or similar, I'm not seeing much reason to add a > new static to allow insertsion sort or similar... > > Can you expand on use cases and why the above aren't sufficient? > > -- T.J. Crowder > _______________________________________________ > es-discuss mailing list > [email protected] > https://mail.mozilla.org/listinfo/es-discuss >
_______________________________________________ es-discuss mailing list [email protected] https://mail.mozilla.org/listinfo/es-discuss

