javascript-tutorial / en.javascript.info

Modern JavaScript Tutorial

Home Page:https://javascript.info

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Lacking arguments in array.sort()'s compareFn

serifold opened this issue · comments

Hello, I found this weird usage in test Shuffle an array's "simple solution":

image

There is just no arguments in the array.sort()'s compareFn:

array.sort(() => Math.random() - 0.5);

But MDN says if we use compareFn, the arguments of compareFn "Will never be undefined.":

image

According to MDN, the compareFn must be used with arguments, i.e.

array.sort((a, b) => Math.random() - 0.5);

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

In my opinion, the shuffle function has nothing to do with the elements of the array since it isn't actually sorting the array. Therefore, it does not need to be passed an argument.

In my opinion, the shuffle function has nothing to do with the elements of the array since it isn't actually sorting the array. Therefore, it does not need to be passed an argument.

I don't think so. The shuffles function does sort the array actually, although not in a ascending or descending order which are the mostly used orders, but in a random order.