Skip to content

Better Array.sort compareFn docs #33372

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

Merged
merged 5 commits into from
Sep 25, 2019
Merged

Better Array.sort compareFn docs #33372

merged 5 commits into from
Sep 25, 2019

Conversation

m1gu3l
Copy link
Contributor

@m1gu3l m1gu3l commented Sep 11, 2019

Fixes #33046

@orta
Copy link
Contributor

orta commented Sep 11, 2019

This looks good (minus the CI failing, should be easy to fix though)

I wonder if it's also worth adding an example with a version for numbers, given how often that's going to be the code people are looking for?

Screen Shot 2019-09-11 at 2 58 52 PM

@m1gu3l
Copy link
Contributor Author

m1gu3l commented Sep 12, 2019

@orta I'm not sure if it's the most common case to be used as example. If you have numbers or strings, you don't usually use custom compareFn (unless you want them to sort in descending order, but this would be misleading as example). Maybe something like this?

(a, b) => a.index - b.index

@orta
Copy link
Contributor

orta commented Sep 12, 2019

Afraid not, wish it were true:

$ node

> [1,2,11,23,3,5].sort()
[ 1, 11, 2, 23, 3, 5 ]

> [1,2,11,23,3,5].sort((lhs, rhs) => lhs - rhs)
[ 1, 2, 3, 5, 11, 23 ]

Because it's alphabetical, not numerical - I've made this mistake a lot!

@m1gu3l
Copy link
Contributor Author

m1gu3l commented Sep 17, 2019

@orta you're right, of course - and certainly have a point that example is needed

@orta
Copy link
Contributor

orta commented Sep 25, 2019

Alright - that's the baselines updated (basically compiler snapshots which got updated because the line references changed) and I've merged master.

Will merge this when it greens. Thanks @m1gu3l :D

@orta orta merged commit 4ee251b into microsoft:master Sep 25, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Improve jsdoc for Array.prototype.sort
2 participants