Proposal: Static sort method on Array

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

Proposal: Static sort method on Array

Rob Ede
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()`.

_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss

signature.asc (849 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Proposal: Static sort method on Array

Claude Pache


> Le 7 avr. 2018 à 21:59, Rob Ede <[hidden email]> 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
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Proposal: Static sort method on Array

C. Scott Ananian
In reply to this post by Rob Ede
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 <[hidden email]> wrote:


> Le 7 avr. 2018 à 21:59, Rob Ede <[hidden email]> 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
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss


_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Proposal: Static sort method on Array

T.J. Crowder-2
In reply to this post by Rob Ede
On Sat, Apr 7, 2018 at 8:59 PM, Rob Ede <[hidden email]> 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
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Proposal: Static sort method on Array

Isiah Meadows-2
When I need a non-in-place sort, I just do `array.slice().sort()`.
It's pretty easy, and it still chains. (In my experience, it's rarely
necessary considering most chaining methods like `.map` and `.filter`
already return new instances.)
-----

Isiah Meadows
[hidden email]

Looking for web consulting? Or a new website?
Send me an email and we can get started.
www.isiahmeadows.com


On Sat, Apr 7, 2018 at 6:18 PM, T.J. Crowder
<[hidden email]> wrote:

> On Sat, Apr 7, 2018 at 8:59 PM, Rob Ede <[hidden email]> 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
> [hidden email]
> https://mail.mozilla.org/listinfo/es-discuss
>
_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Proposal: Static sort method on Array

Naveen Chawla
In reply to this post by T.J. Crowder-2
`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 <[hidden email]> wrote:
On Sat, Apr 7, 2018 at 8:59 PM, Rob Ede <[hidden email]> 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
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss

_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
Reply | Threaded
Open this post in threaded view
|

Re: Proposal: Static sort method on Array

kai zhu
if you want to adhere to the python/jslint philosophy of “there should be one and preferably only one common design-pattern to do it”, then array.from is the most suitable candidate for copying/coercing lists.  it can generalise to common pseudo-lists like function-arguments and frontend-query-selectors, which array.slice cannot as shown in these real world examples [1] [2]:

```js
/*
 * coerce/copy frontend query-selector pseudo-list to list
 */

// disable <script> tag
Array.from(
    document.querySelectorAll('script')
).forEach(function (element) {
    element.outerHTML = '<script></script>';
});
```

```js
/*
 * coerce/copy function-argument pseudo-list to list
 */

task.onDone = function () {
    ...
    // preserve error.message and error.stack
    task.result = JSON.stringify(Array.from(arguments)
        .map(function (element) {
            if (element && element.stack) {
                element = local.objectSetDefault(local.jsonCopy(element), {
                    message: element.message,
                    name: element.name,
                    stack: element.stack
                });
            }
            return element;
        }));
```


On 8 Apr 2018, at 11:12 AM, Naveen Chawla <[hidden email]> wrote:

`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 <[hidden email]> wrote:
On Sat, Apr 7, 2018 at 8:59 PM, Rob Ede <[hidden email]> 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
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss
_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss


_______________________________________________
es-discuss mailing list
[hidden email]
https://mail.mozilla.org/listinfo/es-discuss