javascript-tutorial / javascript-tutorial/en.javascript.info

Lacking arguments in array.sort()'s compareFn

Open
#3,562 2 comments 0 reactions 0 assignees View on GitHub
Dominant language
HTML
Stars
25.5k
Forks
4k
PR merge metrics
No merged PRs in 30d

Description

Hello, I found this weird usage in test [Shuffle an array](https://javascript.info/array-methods#shuffle-an-array)'s "simple solution":

![image](https://github.com/javascript-tutorial/en.javascript.info/assets/80401151/678c9a62-033f-40db-b9aa-2d7b57c62e3b)

There is just no arguments in the `array.sort()`'s `compareFn`:
```js
array.sort(() => Math.random() - 0.5);
```

But [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort#parameters) says if we use `compareFn`, the arguments of `compareFn` "Will never be undefined.":

![image](https://github.com/javascript-tutorial/en.javascript.info/assets/80401151/27735ec7-2e97-4fb4-8aee-f9b2fa2d7d76)

According to MDN, the `compareFn` must be used with arguments, i.e.
```js
array.sort((a, b) => Math.random() - 0.5);
```

If it is a typo indeed, please fix it at your convenience, thank you.

Contributor guide

No contributing guide indexed for this repository

Research direction

Start with the “Shuffle an array” section linked in the issue and search the tutorial source for the shown array.sort() example. Confirm the compareFn wording against the referenced MDN documentation, then update the example if it is a typo and verify that the tutorial displays the corrected snippet.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
documentation
Issue type
Documentation
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.