Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make Array.sort Order arg optional to match js implementation. #2600

Open
jessekelly881 opened this issue Apr 23, 2024 · 3 comments
Open

Make Array.sort Order arg optional to match js implementation. #2600

jessekelly881 opened this issue Apr 23, 2024 · 3 comments
Labels
enhancement New feature or request

Comments

@jessekelly881
Copy link
Contributor

jessekelly881 commented Apr 23, 2024

What is the problem this feature would solve?

Array.prototype.sort's compare function is optional and it automatically handles sorting number[], string[], etc. I think we should do the same with Array.sort(et al) making the Order arg optional and relying on the default whenever possible. Array.sort() is more convenient and would be more performant than Array.sort(Order.number) or Array.sort(Order.string)

What is the feature you are proposing to solve the problem?

^

What alternatives have you considered?

No response

@jessekelly881 jessekelly881 added the enhancement New feature or request label Apr 23, 2024
@jessekelly881 jessekelly881 changed the title Make Array.sort order arg optional to match js implementation. Make Array.sort Order arg optional to match js implementation. Apr 23, 2024
@mikearnaldi
Copy link
Member

Array.sort is mutable so it can't be used directly, we'd have to clone the array leading to the same perf tradeoff

@jessekelly881
Copy link
Contributor Author

jessekelly881 commented Apr 23, 2024

The mutability of .sort isn't relevant here though. I'm only suggesting that we change Array.sort(o: Order) to Array.sort(o?: Order) without changing the implementation. A compare fn isn't required when using either Array.prototype.sort or Array.prototype.toSorted unless you want to override the default behavior.

["b","a","c"].sort(Order.string) // ["a", "b", "c"]
["b","a","c"].sort() // ["a", "b", "c"]

@mikearnaldi
Copy link
Member

would be more performant than Array.sort(Order.number) or Array.sort(Order.string)

was answering to:

would be more performant than Array.sort(Order.number) or Array.sort(Order.string)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants